0

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!

3
Contributors
3
Replies
7
Views
6 Years
Discussion Span
Last Post by madz015
0

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 :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.