Jump to content

[SOLVED] How to force sessions to be active until logoff or a period of inactivity?


lilwing

Recommended Posts

It is likely that the session is not really starting on the first page or you are not starting it on the next page. In any case, post your code to get specific help with what it is or is not doing.

by "visit another page" do you mean some other external page, or something internal on your own site?  If it's the first, you can set a cookie and check for it on page loads.  If it's the second...do you have session_start() at the top of the page in question?

Do you have to include session_start on every page?

 

 

this is the php on the first page:

<?php

session_start();

$_SESSION['logged'] = false;
$_SESSION['uid'] = 0;
$_SESSION['username'] = '';

?>

 

Then there is a verification page, which redirects to the second page:

 

<?php
ob_start();

//Login info
$host="";
$sqlusername="";
$sqlpassword="";

// Connect to server
mysql_connect("$host", "$sqlusername", "$sqlpassword")or die("Unable to connect");

//SQL info
$db_name="";
$tbl_name="";

//Select database
mysql_select_db("$db_name")or die("Unable to select database");

//Assign post data from the login page to a variable
$username=$_POST['username'];
$password=$_POST['password'];

$passwordencrypted = md5($password);


//Variable containing SQL instruction
$verification="SELECT * FROM $tbl_name WHERE username='$username' and password='$passwordencrypted'";
//Variable containing Query of SQL instruction
$result=mysql_query($verification);

//Variable containing # of query matches
$count=mysql_num_rows($result);

//If username and password are correct...
if($count==1)
{
	//Register username, password and redirect to page desired
	_setSession();
	header("location:basicadminpage.php"); //insert location
}
else 
{
	//If username and password are incorrect...
	echo "Access denied";
}

//End output buffer

mysql_close();

ob_end_flush();

?>

 

and the 2nd page

<?php

if (defined('SID'))
        {
        echo 'Session is active';
        }
else
        {
        echo 'Session is not started';
        }

?>

Okay, I have session_start on each page. Here is how I am testing it:

 

<?php

session_start();

$_SESSION['logged'] = false;
$_SESSION['uid'] = 0;
$_SESSION['username'] = '';

?>

 

<?php

session_start();

if ($_SESSION['uid']=0)
{ echo "fuck fuck fuck!";}

if (defined('SID'))
        {
        echo 'Session is active';
        }
else
        {
        echo 'Session is not started';
        }


?>

 

If the test worked, it should have echoed "fuck fuck fuck!" But I am not sure what I did wrong.

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.