Hi
It is the php code for session created in login page with enter button:
<?php
ob_start();
$login=$_POST['enter'];
if($login)
{
$username=$_POST['username'];
$password=$_POST['password'];
//...code to connect to DB
$result=mysql_query("select username, password from user where username='$username' and password='$ppassword'");
if(mysql_num_rows($result)!='0')
{
$_SESSION['log'] = 1;
$_SESSION['username'] = $username;
while (ob_get_level())
ob_end_clean();
header("location: 1st.php");
ob_end_flush();
exit();
}
}
?>
It is the php code for getting session in after login page, i.e. 1st.php
<?php
session_start();
ob_start();
if ($_SESSION['log'] !== 1) {
unset($_SESSION['log']);
header('location: register.php');
ob_end_flush();
exit();
}
else
{
if(isset($_SESSION['username']))
{
$username = $_SESSION['username'];
//below is the program code to use the $username variable.
I only use the above code for session username.
I have many php pages using the $username variable.
When different users login to the program, sometimes, not each time, they will view other people's info.
e.g., User A login, 1st page will display "Hello, Mr A, welcome to my website".
However, I tried that when User A login, it shows "Hello, Mr B, welcome to my website". and shows Mr B's info.
It made me crazy. Any mistake of the session code?
I use php 5. I have to use ob_start() and ob_end_clean(); otherwise, it will have error page.