PDA

View Full Version : PHP login script tutorial


Piro
08-18-2007, 09:08 PM
First i would like to say that the php tutorials are excellent they have helped me learn and understand a lot about php but i seem to be having a bit of trouble with sessions. i have a members panel that i would like to link to other php pages but the code

if(!isset($_SESSION['id'])){
header ("location:loginpage.php");
echo "ACCESS DENIED!!";
exit;
}

does not work properly for all the pages when i try to use it in an include or just add it to the new page it gives me a mysql error that the headers have already been set is there some way i need to change or edit this code for the session to continue through to the linked pages? Any help would be fantanstic. Sorry im such a nub lol.

davidj
08-19-2007, 02:04 AM
ok you have this...

if(!isset($_SESSION['id'])){ //<< check the session is empty
header ("location:loginpage.php"); //<< redirect if it is
echo "ACCESS DENIED!!"; // << echo a message if it is
exit; // << exit script
}

how can you redirect to a different page and at the same time echo a message?

also a tip...

never echo within the main PHP and above any HTML. This is why your getting "headers already sent" errors.

best to assign any message within an IF statement to a variable then echo it inside the HTML

Piro
08-19-2007, 01:31 PM
ok i took out the echo statement and i still get the same message
Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\Piro\My Documents\web root\test\rfeditmain.php:2) in C:\Documents and Settings\Piro\My Documents\web root\test\rfeditmain.php on line 7

header ("location:rfloginpage.php");
with that as line 7 ive also tried using session register isntead of start that doesnt work either not sure what i should do to fix this

Piro
08-19-2007, 01:43 PM
never mind must have been something about the way i coded that page it works fine with some others i tested thnx for the info anyways