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>"; ?> Link to comment https://forums.phpfreaks.com/topic/94763-session-display/ 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; Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485228 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? Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485241 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. Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485250 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? Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485263 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 Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485273 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 Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485278 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; Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485289 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 Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485300 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??? Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485307 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 Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485317 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 Link to comment https://forums.phpfreaks.com/topic/94763-session-display/#findComment-485320 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.