Session not registering

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
r22pl
Forum Newbie
Posts: 6
Joined: Tue Mar 07, 2006 3:45 pm

Session not registering

Post by r22pl »

I have no idea why this isnt working

I have a login system for my website, and I have it so

Code: Select all

if ($_GET['id'] == "login") {
	if (isset($_SESSION['adminuser'])) {
		echo "You are already logged in!";
	} else {
		// Check Supplied User/Pass
		$login_user = $_POST['login_user'];
		$login_pass = $_POST['login_pass'];
		if ($login_user == "USERNAME" && $login_pass == "PASSWORD") {
			// User Confirmed, register session and redirect
			session_register('adminuser');
			$_SESSION['adminuser'] = $adminuser;
			header('Location: index.php');
		} else {
			// User Denied
			echo "Invalid Login";
		}
	}
}
(the user/pass is USERNAME:PASSWORD)

the session wont register, when i do

Code: Select all

if (isset($_SESSION['adminuser'])) {
	echo "Welcome back!";
}
, no text will appear
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

session_register() is not to be used anymore. Did you remember to call session_start()?
r22pl
Forum Newbie
Posts: 6
Joined: Tue Mar 07, 2006 3:45 pm

Post by r22pl »

Yeah, I have session_start(); in the beginning of my document. What should I use instead of session_register?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

as long as you use $_SESSION, nothing else. You may need to call session_write_close() as using a redirection can short-circuit any session creation.
r22pl
Forum Newbie
Posts: 6
Joined: Tue Mar 07, 2006 3:45 pm

Post by r22pl »

Where should I put it?

So I get rid of the session_register('adminuser'); and add the session_write_close();

Code: Select all

// User Confirmed, register session and redirect
$_SESSION['adminuser'] = $adminuser;
session_write_close();
header('Location: index.php');
because that didn't work
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can just set the session var by creating one...

Code: Select all

<?php
session_start();
$_SESSION['my_session_var'] = 'This is my value';
echo $_SESSION['my_session_var'] . '<br />';

if ( changed_my_mind() ) {
    // Pretend function to show the point...
    $_SESSION['my_session_var'] = 'This is my new session value';
}

echo $_SESSION['my_sesison_var'];
?>
r22pl
Forum Newbie
Posts: 6
Joined: Tue Mar 07, 2006 3:45 pm

Post by r22pl »

Thanks, it fixed the problem :D
Post Reply