Hi Everyone,

Can someone explain to me where I got wrong with my code. I'm trying to pass a session variable from page to page, from the first to second page the value is still there but on the succeeding page the value is gone. I have ensure that the session_start is always at the top of the page that's calling the session variable. The code goes like this

public function register($post) {
		
		$username = $post['username'];
		$password = $post['password'];
		$user = $this->authenticate($username, $password); 
		if(count($user)):
			$_SESSION['userid'] = $user['userid'];
			$_SESSION['username'] = $user['username'];
			$_SESSION['password'] = $user['password'];			
		endif;
		return $user;

On the succeeding page, I call the

$sessionID=$_SESSION['userid'];
echo $sessionID;

and it throws me the value but on the next page when I call it again like the same way as I call it above it returns no value.

Thanks in advance for helping!

Recommended Answers

All 3 Replies

use this to start a session on your function >>

public function register($post) {
		
		$username = $post['username'];
		$password = $post['password'];
		$user = $this->authenticate($username, $password); 
		if(count($user)):
                        #start a session before assigning the session variables
                        session_start();
			$_SESSION['userid'] = $user['userid'];
			$_SESSION['username'] = $user['username'];
			$_SESSION['password'] = $user['password'];			
		endif;
		return $user;

and on the successdding page

session_start();

//check session variable exist  
$sessionID= (isset($_SESSION['userid'])) ? $_SESSION['userid'] : 'Error';
echo $sessionID;

hope this will help you :)

you must call session_start() in all pages, in the beginning of your code.

thank you @jogesh for your help it work :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.