For added security iv coded all my pages so that they can not be access unless there is a session created (successful login). Below is the code i am using when checks to see if the login details the user entered are correct.
As you can see if MySQL returns 1 record then it will create the session called AdminID and give it the value of $adminid. Now iv done checks by echoing the value of the session to make sure a value is added and it exists and it works fine. Now the problem i have is when it takes me to admin.php as i have the following code:
session_start();
if (isset($_SESSION["AdminID"])) {
//THEN
example code here
}
else{
//NO LOGIN - SEND TO LOGIN
header('Location: http://xxxxxxxxxxxxx/index.php');
}
But it keeps taking me back to index.php where i need to login again. Which means that the code is saying that the session does not exist but it does. Can anyone help me please?
If putting session_start() at the top of the page hasn’t fixed the problem then there is a problem somewhere else in your code that you haven’t posted.
And if the echo only outputs
$_SESSION["AdminID"] =
then there is no point saying
Which means that the code is saying that the session does not exist but it does
because it doesn’t.
I can’t see your updated code, so there isn’t much more I can do.
Note: I’ve disabled the redirection so that we can see that the value is being set. I’ve never been one for using the Header to bounce people around, personally.
That works and so does my code as i said another post on here as when i echo the session the output is 1 which is correct. Its when it redirects to the admin.php that causes the problem and i dont know why
Please see my reply above yours, All the code is in login.php and if i place your code under where the session is created then it outputs the session fine. I’ve tried placing the session start right at the top of the page but same thing happens
session_start() should really be at the very top of your php page before any output.
Which means that the code is saying that the session does not exist but it does
In admin.php insert this echo statement to see if the session variable really exists:
session_start();
echo '$_SESSION["AdminID"] = '.$_SESSION["AdminID"];
die();
if (isset($_SESSION["AdminID"])) {
//THEN
example code here
}
else{
//NO LOGIN - SEND TO LOGIN
header('Location: http://xxxxxxxxxxxxx/index.php');
}
Just to add sorry i do have session_start(); at the top of admin.php
No, I meant session_start() should be at the top of the page where this code is in:
if($count==1){
session_start();
The echo is outputting nothing in admin.php because the session variable doesn’t exist and I suspect it doesn’t exist because the session in
if($count==1){
session_start();
hasn’t been created because the session_start() failed because it is not at the top of the page where it normally should be.
If any output has occured (including blank lines) before session_start() is called, session_start() will fail.