Jump to content

session display


DarkPrince2005

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.