DarkPrince2005 Posted March 6, 2008 Share Posted March 6, 2008 Haha, I finally got sessions working with a login for the first time, but can somebody help me? I want to modify the script sothat if a session hasn't been started or doesn't exist that the page must display a message with a link to the login page. Here is the code: <?php ini_set('display_errors','On'); error_reporting(E_ALL); ini_set('session.cache_limiter',''); mysql_connect("localhost","root",""); mysql_select_db("db"); $sql=mysql_query("select * from table"); echo " <html> <head> <title></title> <style> .scrollable { width: 100%; height: 500px; overflow: auto; background-repeat:no-repeat; } td{background-repeat: no-repeat; } </style> </head> <body topmargin='0' bottommargin='0' rightmargin='0' leftmargin='0' bgcolor='#FFFFFF'> <center><table cellspacing='0' cellpadding='0' border='0' height='100%'> <tr> <td width='1024' height='75' background='top.jpg' colspan='2'> </td> </tr> <tr> <td width='1024' height='20' background='top1.jpg' colspan='2' align='right' valign='center'><font size='3'>"; session_start(); echo "Currently logged in as <b>$_SESSION[usernam]</b> | <a href='logout.php'><font color='#343635'><b>Logout<b></font></a> </td> </tr> <tr> <td width='1024' height='13' background='top2.jpg' colspan='2'> </td> </tr> <tr> <td width='168' background='nav.jpg'> </td> <td width='856' background='main.jpg' align='center' valign='top'><br><div class='scrollable'> <table cellpadding='2' border='0' cellspacing='0' width='750'> <tr><td> </td> <td align='center' width='100'><b>Client ID</b></td> <td align='center' width='100'><b>Surname</b></td> <td align='center' width='100'><b>Firstname</b></td> <td align='center' width='150'><b>Training Program</b></td> <td align='center' width='150'><b>Date Attended</b></td> <td> </td> </tr>"; $counter = 0; while ($row = mysql_fetch_array($sql)) { echo "<form method='post' action='clientview.php'><tr><td valign='top'>".++$counter." . </td> <td align='center'><input type='hidden' name='clientid' value='$row[clientid]'>$row[clientid]</td> <td align='center'><input type='hidden' name='surname' value='$row[surname]'>$row[surname]</td> <td align='center'><input type='hidden' name='firstname' value='$row[firstname]'>$row[firstname]</td> <td align='center'>$row[trainingprogramme]</td> <td align='center'>$row[dateattended]</td> <td align='center'><input type='image' src='view.gif' value='Submit' alt='Submit'> </tr></form>"; }; echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 6, 2008 Share Posted March 6, 2008 When the user succesfully logs in, set a session variable called isLoggedIn to true, eg: $_SESSION['isLoggedIn'] = true; Now on each page that you require the user to be logged in, add the following code at the beginning of the file: <?php session_start(); if(!isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn'] != true) { die(' You must be logged in in order to view this page! '); } // rest of code for page here When the user log's out make sure you unset the isLoggedIn session variable, eg: unset($_SESSION['isLoggedIn']); // OR $_SESSION['isLoggedIn'] = false; Quote Link to comment Share on other sites More sharing options...
DarkPrince2005 Posted March 6, 2008 Author Share Posted March 6, 2008 That's all good, but what'll happen once the period that each session exists expires? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 6, 2008 Share Posted March 6, 2008 Once a session has expired PHP will generate a new empty session and so when a user goes to a page which requires loggin in it'll display the "You must be logged in in order to view this page!" error. PHP won't carry over the old session data to the new session. Quote Link to comment Share on other sites More sharing options...
DarkPrince2005 Posted March 6, 2008 Author Share Posted March 6, 2008 But, currently it displays this error: Notice: Undefined index: isLoggedIn in C:\xampp\htdocs\Colleen\view.php on line 6 You must be logged in in order to view this page! Will the error you're talking about look the same? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 6, 2008 Share Posted March 6, 2008 Sorry, try: <?php session_start(); $_SESSION['isLoggedIn'] = false; if(!isset($_SESSION['isLoggedIn']) || (isset($_SESSION['isLoggedIn']) && $_SESSION['isLoggedIn'] != true)) { die(' You must be logged in in order to view this page! '); } // rest of code for page here That should stop the notice Quote Link to comment Share on other sites More sharing options...
DarkPrince2005 Posted March 6, 2008 Author Share Posted March 6, 2008 Now it doesn't show even if I am logged in, because it keeps on reading the session isLoggedIn as false Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 6, 2008 Share Posted March 6, 2008 Bulls I forgot to remove the following line whilst testing, sorry $_SESSION['isLoggedIn'] = false; Quote Link to comment Share on other sites More sharing options...
DarkPrince2005 Posted March 6, 2008 Author Share Posted March 6, 2008 Bloody hell, i think i got it to work, but i need to bloody refresh the page everytime, isn't there a way that you can do that in code Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 6, 2008 Share Posted March 6, 2008 Bloody hell, i think i got it to work, but i need to bloody refresh the page everytime, isn't there a way that you can do that in code What don't understand??? Quote Link to comment Share on other sites More sharing options...
DarkPrince2005 Posted March 6, 2008 Author Share Posted March 6, 2008 the system keeps loading the cached page Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 6, 2008 Share Posted March 6, 2008 Have a read of the following article to prevent browsers from caching your pages Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.