PHP login script – Possible session errors
Question by Michael Garrison
I am working on a login system for a client and I have run into an issue that I am unsure how to fix. I have it set so that when the user tries to access the index.php of a certain directory they are require to login. The code that I have doing this is:
<?php
session_start();
if(!$_SESSION['myusername']){
header("location:Login_admin.php");
}
?>
On the Login_admin.php page, there is a form with the action=”checklogin.php”. here is where it connects to the database and starts a session where it stores the username:
if($count==1){
session_start();
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'];
$_SESSION['mypassword'];
header("location:index.php");
}
Originally the above code did not have the session_start() so I added that in in attempt to solve my problem.
The actual problem is the session is not registering. If I go to one of the other pages in the director (all of them have that first segment of code at teh top) and log in it sends me to index.php like I want, but when I try to go back to that page it makes me log in again. At one point I had that first segment of code on my index page and even with the correct log in it would loop back to the login page.
I was shown this script by a friend and have not really changed much. Originally the script had:
session_register("myusername");
But after some debugging, I changed it to:
$_SESSION['myusername'];
A final note, I am not an expert the issue is probably considered a silly mistake but for the life of me I can’t figure it out.
Thanks in advance!
Answer by Your Common Sense
the
$_SESSION['myusername'];
line does absolutely nothing
to assign a value to session variable you have to assign a value to session variable
the usual way values being assigned to variables in PHP
Answer by Starx
You have to give a value to each session variable
$_SESSION['myusername'] = 'thename';
$_SESSION['password'] = '*******';