Login php sessions

This is a discussion on Login php sessions within the PHP Language forums, part of the PHP Programming Forums category; Im having a problem, which in my head seems to be simple but i cannot figure it out. there are ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-28-2005
Quinonez
 
Posts: n/a
Default Login php sessions

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

Reply With Quote
  #2 (permalink)  
Old 04-28-2005
MsKitty
 
Posts: n/a
Default Re: Login php sessions

You need to register the session variable before you set it, thus:
session_register("username");
$_SESSION['username'] = $username;


Kitty
http://OpenSkyWebDesign.com

Reply With Quote
  #3 (permalink)  
Old 04-28-2005
Quinonez
 
Posts: n/a
Default Re: Login php sessions

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?

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:18 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0