How to Store a Variable in PHP Using Session

How to store a variable in php using session

at the top of page

session_start();

then to set session variable

$_SESSION['id'] = $someID;

To retrieve the value of id

$pid = $_SESSION['id'];

Further more read more about session here

How to use store and use session variables across pages?

Reasoning from the comments to this question, it appears a lack of an adjusted session.save_path causes this misbehavior of PHP’s session handler. Just specify a directory (outside your document root directory) that exists and is both readable and writeable by PHP to fix this.

After using a form POST how can I store the variable in session?

Yes you can store it in SESSION. Please read the following code:-

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Check Post variables are available
if(isset($_POST['username']))
{
echo $_POST['username']." Username found in form <br />";
// Set session variables
$_SESSION["username"] = $_POST['username'];
echo $_SESSION["username"]." stored in session <br />";;
}
else
echo 'No, form submitted. Your old stored username was '.$_SESSION["username"];
//echo 'No, form submitted.';
?>

</body>
</html>

To start session in wordpress

Write below code in your functions.php

function register_my_session()
{
if( !session_id() )
{
session_start();
}
}

add_action('init', 'register_my_session');

How to store variable in session codeigniter

Simple :

First, load this library

$this->load->library('session');

Then, to add some informations in session :

$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe@some-site.com',
'logged_in' => TRUE
);

$this->session->set_userdata($newdata);

Next, if you want to get values :

$session_id = $this->session->userdata('session_id');

And to remove :

$this->session->unset_userdata('some_name');

A simple search "codeigniter session" could have help you ...

https://www.codeigniter.com/user_guide/libraries/sessions.html

Don't forget to upvote and mark as solved if you find this useful :)



Related Topics



Leave a reply



Submit