Jump to content

[SOLVED] Session will only remember one variable..


Sesquipedalian

Recommended Posts

Hey everyone, I'm trying to get back into PHP, so I'm making a really simple login (not too secure either).. The only problem is, it doesn't seem to remember one of the session variables...

 

index.php:

<?
session_start();
include ('homepage.php');
?>

 

 

homepage.php:

<?
include ('class.php');
echo '<h1>Login</h1><br />';

if (!$_POST && !$_GET) {
if (!isset($_POST['user']) && !isset($_POST['pass'])) {
	$login->disp_form();
}
}
else if (isset($_POST['user']) && isset($_POST['pass'])) {
$login->check('sesq', md5('sesq'), $_POST['user'], md5($_POST['pass']));
}
else if ($_GET['login'] == 'accepted') {
	include ('links.php');
}
?>

 

class.php:

<?
class login {
function disp_form() {
	echo '<center><form action="?login=submit" method="POST">'
		.'<input type="text" name="user"></input><br />'
		.'<input type="password" name="pass"></input><br />'
		.'<input type="submit" value="Login" />';
}
function check($user, $pass, $form_user, $form_pass) {
	if ($user == $form_user && $pass == $form_pass) {
		$_SESSION['login'] = 'yes';
		$_SESSION['user'] = $user;
		echo '<center><a href="?login=accepted">Proceed...</a>';
	} else {
		echo 'Incorrect.';
	}
}
}	
$login = new login();

?>

 

 

links.php:

<?
echo '<center>Welcome, '.$_SESSION['user'];
?>

 

It displays $_SESSION['user'] as sesq, which its supposed to.. but if I try to make it echo $_SESSION['login'] it displays Object id #1 and not 'yes' like it should. $_SESSION['login'] is what I'm going to determine if they are logged in. I know I could use $_SESSION['user'] to check if they are logged in, but I still want to know why it doesn't seem to maintain the fact that $_SESSION['login'] == 'yes'?

 

Thanks,

 

-

Sesq

Are you putting session_start(); in links.php?

session_start(); Must be on every single page you want to register or use sessions in any way.

 

I've tried that before, and it doesn't seem to matter.

 

Besides, if that was the problem, why does can it remember what $_SESION['user'] is?

 

I changed it to make it $_SESSION['auth'], and it works.

 

Still curious of why it wouldn't work though with $_SESSION['login']?

<?php
function session($user, $login = 'yes'){
$_SESSION['user'] = $user;
$_SESSION['login'] = $login;
}

session('marcus');

echo "<pre>\n";
print_r($_SESSION);
echo "</pre>\n";

session('billy','no');

echo "<pre>\n";
print_r($_SESSION);
echo "</pre>\n";
?>

 

My results:

 

Array
(
    [user] => marcus
    [login] => yes
)

Array
(
    [user] => billy
    [login] => no
)

If you are having variables overwritten (and when you change the name of one of them it works), it is usually due to register_globals being on.

 

A) Use unique variable and array index names where register_globals are on.

 

B) If register globals are on, after you make sure none of the rest of your code is dependent on them being on, turn register globals OFF.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.