By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,409 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,409 IT Pros & Developers. It's quick & easy.

Login php sessions

P: n/a
Im having a problem, which in my head seems to be simple but i cannot
figure it out.

there are 3 pages.

Page 1 (login form)

page 2 (verifies login/pass and begins session. has link to page 3)

Page 3 (displays some info has a link back to page 2)

obviously i only want pages 2 and 3 to be able to be accessed through
the login.

i can get to page 2 and page 3 after login no problem but IF i press
the link on page 3 to page 2 it takes me back to page 1 and i have to
login again.

here is the code

Page 1 (admin.php)
......

<form method="post" action="edit_site.php">
Name: <input type="text" name="username">
Password: <input type="text" name="password">
<input type="submit" value="ENTER">
</form>
......

Page 2 (edit_site.php)

<?php
session_start();

$db_user = 'database_user';
$db_pass = 'database_password';
$username = $_POST['username'];
$password = $_POST['password'];
$connection = mysql_connect('localhost', $db_user, $db_pass) or
die(mysql_error());
mysql_select_db('database_main', $connection) or die(mysql_error());
$query = "SELECT * FROM users
WHERE username='$username' AND password='$password'";

$result = mysql_query($query, $connection) or die('error making
query');
$affected_rows = mysql_num_rows($result);

if($affected_rows == 1) {
$_SESSION['username'] = $username;
}
else {
header( "Location: admin.php" );
}

?>

Page 3 (edit_news.php)
<?php
session_start();
if(session_is_registered('username')){

echo 'Welcome, you are still logged in.';

}
else{
header( "Location: admin.php" );
}
?>

any help is GREATLY appreciated. thank you

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You need to register the session variable before you set it, thus:
session_register("username");
$_SESSION['username'] = $username;
Kitty
http://OpenSkyWebDesign.com

Jul 17 '05 #2

P: n/a
i entered that here

Page 2 (edit_site.php)
.....
if($affected_rows == 1) {
session_register("username");
$_SESSION['username'] = $username;
}
else {
header( "Location: admin.php" );
}

?>

doesnt work? is that correct, Shouldnt there be something when i come
back that checks if the session is still good?

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.