Jump to content
Killersites Community
Sign in to follow this  
AJohn360

Please Help on Php session

Recommended Posts

Please can anyone help me on this project i am new to php and i don't know how to go about this project i would really appreciate anyone that can help me on this thank you and God bless .

 

 

I am creating a application using php and i want the application to do the following .

 

> i have a Users pager and i want all user to have a access code which i have done my self

 

> i want the access code to expires every 20min of which the user is login and the page should timeout in 20min

>if user did not finish the 20min and want to logout the timer should stop and when ever the user log in back the time should start from the last logout time

> the time should be able to display on the user page

>when the page logout after 20min the access code should be invalid so that the user would not be able to use the access code any more.

Share this post


Link to post
Share on other sites

Hi,

 

This would be a combination of PHP and JavaScript using AJAX to bridge the two. It would take some thinking on how to exactly engineer this ...

 

Stef

Share this post


Link to post
Share on other sites

I did a video tutorial series on creating a basic PHP based login system that's available to subscribers within the KillerSites Video Library (http://killersites.com/university/ - under PHP > PHP Login). It doesn't cover absolutely everything you want, but should get you partway there. I discuss creating login/logout functionality, adding/editing/removing users, user roles (like "regular" members and "admin" members) and how to make the session expire at a specified time.

 

However, I'm not tracking that expiration time so that users can see it. You'd probably have to do that with a combination of PHP and Javascript, using PHP to store the current time when the page was first loaded, saving it to a database (or storing the time remaining when the user logs out), and using Javascript to count down the time and call an ajax function to log the user out once the time expires. The downside to this though (and as far as I can think of at the moment, there isn't really any way around this), is that this setup would require Javascript to work properly. If someone decides to browse the site without Javascript, or purposely turns it off, the user could stay on the page for more than the specified time.

Share this post


Link to post
Share on other sites

I did a video tutorial series on creating a basic PHP based login system that's available to subscribers within the KillerSites Video Library (http://killersites.com/university/ - under PHP > PHP Login). It doesn't cover absolutely everything you want, but should get you partway there. I discuss creating login/logout functionality, adding/editing/removing users, user roles (like "regular" members and "admin" members) and how to make the session expire at a specified time.

 

However, I'm not tracking that expiration time so that users can see it. You'd probably have to do that with a combination of PHP and Javascript, using PHP to store the current time when the page was first loaded, saving it to a database (or storing the time remaining when the user logs out), and using Javascript to count down the time and call an ajax function to log the user out once the time expires. The downside to this though (and as far as I can think of at the moment, there isn't really any way around this), is that this setup would require Javascript to work properly. If someone decides to browse the site without Javascript, or purposely turns it off, the user could stay on the page for more than the specified time.

 

Thank you Ben for your response . i understand the login timeout from the video and how to set a session to timeout as well using java script and php but i don't know how to store to time of which the user sign in the data base and logout time too. and that is the major problem i have. also to store the session in the database as well so that the database would know when the user is out and in.

 

or is there another way i can make the above problem work??

Share this post


Link to post
Share on other sites

To get the current time you can either use the $_SERVER['REQUEST_TIME'] variable (available since PHP 5.1.0) or the time() function to get the current Unix timestamp.

 

When you originally create the username/password combination, you would need to have a column in the database to store any time remaining, and you'd initially set that to 20 minutes.

 

You'd need to follow these basic steps:

 

-- When the user first logs in, you would need to check the database to ensure the user had time available. If so, you would need to retrieve the amount of time available from the database.

-- Then, you would use PHP to get the current time, and save that in a session variable to indicate when the page was first loaded.

-- You would need to use Javascript to display a countdown clock that counted down the total amount of time remaining that you pulled from the database.

-- If the user logged out, you would get the current time again, compare that against the time when the page was first loaded, and update the remaining time in the database appropriately.

-- If the time runs out, you would need to use javascript/Ajax to set the amount of remaining time in the database to 0, and to redirect the user away from the current page.

-- If the user closes the page, you would need to use AJAX to call PHP code that will get the current time again, compare that against the time when the page was first loaded, and update the remaining time in the database appropriately. This page talks about how to prompt just before the page is closed, and should lead you in the right direction: http://stackoverflow.com/questions/333665/javascript-to-get-an-alert-when-closing-the-browser-window

 

 

 

I don't know how experienced you are with PHP and Javascript/AJAX, but to be completely honest, this is a relatively difficult thing for someone brand new to PHP. Either you are going to have to work your way up to it, practicing PHP tutorials until you can understand things enough to write this yourself (see http://www.killervideostore.com, http://killerphp.com/, http://phpvideotutorials.com/ and the free Diving into PHP series http://net.tutsplus.com/articles/web-roundups/who-needs-university-the-best-nettuts-screencast-training-courses/ to learn PHP) or you will need to pay someone to do this for you. You probably aren't going to find someone who is going to write the code for you for free -- not for something like this which will take a bit of time to put together.

Share this post


Link to post
Share on other sites

To get the current time you can either use the $_SERVER['REQUEST_TIME'] variable (available since PHP 5.1.0) or the time() function to get the current Unix timestamp.

 

When you originally create the username/password combination, you would need to have a column in the database to store any time remaining, and you'd initially set that to 20 minutes.

 

You'd need to follow these basic steps:

 

-- When the user first logs in, you would need to check the database to ensure the user had time available. If so, you would need to retrieve the amount of time available from the database.

-- Then, you would use PHP to get the current time, and save that in a session variable to indicate when the page was first loaded.

-- You would need to use Javascript to display a countdown clock that counted down the total amount of time remaining that you pulled from the database.

-- If the user logged out, you would get the current time again, compare that against the time when the page was first loaded, and update the remaining time in the database appropriately.

-- If the time runs out, you would need to use javascript/Ajax to set the amount of remaining time in the database to 0, and to redirect the user away from the current page.

-- If the user closes the page, you would need to use AJAX to call PHP code that will get the current time again, compare that against the time when the page was first loaded, and update the remaining time in the database appropriately. This page talks about how to prompt just before the page is closed, and should lead you in the right direction: http://stackoverflow.com/questions/333665/javascript-to-get-an-alert-when-closing-the-browser-window

 

 

 

I don't know how experienced you are with PHP and Javascript/AJAX, but to be completely honest, this is a relatively difficult thing for someone brand new to PHP. Either you are going to have to work your way up to it, practicing PHP tutorials until you can understand things enough to write this yourself (see http://www.killervideostore.com, http://killerphp.com/, http://phpvideotutorials.com/ and the free Diving into PHP series http://net.tutsplus.com/articles/web-roundups/who-needs-university-the-best-nettuts-screencast-training-courses/ to learn PHP) or you will need to pay someone to do this for you. You probably aren't going to find someone who is going to write the code for you for free -- not for something like this which will take a bit of time to put together.

 

Hello Ben, i have been working on the step you listed above and i have been doing some few things but here i have some few problems. checking my users if they have time left . i have a login page with email as username and access code as password and i have a colum in the database where i store time called expires_time . when i run the the login page and enter a username in the data and a access code to that username which i set the expires_time to 20min .

codes below:

 

database table :subscription

email: data type = %s

access code : data type =%s

expires_time data type = date

 

 

<?php require_once('../Connections/con_db.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

if (PHP_VERSION < 6) {

$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

}

 

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

switch ($theType) {

case "text":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "long":

case "int":

$theValue = ($theValue != "") ? intval($theValue) : "NULL";

break;

case "double":

$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

break;

case "date":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "defined":

$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

break;

}

return $theValue;

}

}

?>

<?php

// *** Validate request to login to this site.

if (!isset($_SESSION)) {

session_start();

}

 

$loginFormAction = $_SERVER['PHP_SELF'];

if (isset($_GET['accesscheck'])) {

$_SESSION['PrevUrl'] = $_GET['accesscheck'];

}

 

if (isset($_POST['email'])) {

$loginUsername=$_POST['email'];

$password=$_POST['roll_no'];

$MM_fldUserAuthorization = "";

$MM_redirectLoginSuccess = "members.php";

$MM_redirectLoginFailed = "makesub.php";

$MM_redirecttoReferrer = true;

mysql_select_db($database_con_db, $con_db);

 

$LoginRS__query=sprintf("SELECT email, roll_no, date (expires_time) As expires_time FROM subscription WHERE email=%s AND roll_no=%s",

GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

 

$LoginRS = mysql_query($LoginRS__query, $con_db) or die(mysql_error());

$loginFoundUser = mysql_num_rows($LoginRS);

if ($loginFoundUser) {

 

$expires = '20:00:00';

//compare the expiry time with $expires

if ( $expires > $LoginRS['expires_time'] )

{

header('location:makesub2.php');

exit;

 

}

$loginStrGroup = "";

 

//declare two session variables and assign them

$_SESSION['MM_Username'] = $loginUsername;

$_SESSION['MM_UserGroup'] = $loginStrGroup;

 

if (isset($_SESSION['PrevUrl']) && true) {

$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];

}

header("Location: " . $MM_redirectLoginSuccess );

}

else {

header("Location: ". $MM_redirectLoginFailed );

}

}

?>

 

the code is only working

1 if the user expires_time is set to 00:00:00

 

i want the code to also work if the user have time left but not more than 20min set in the expires_time in the database.

Any one can help me with this i would be much more happy thank you .

Share this post


Link to post
Share on other sites

What happens currently when the expires_time is set to something other than 00:00:00? What are you expecting, and what are you getting? (If you are getting error messages, can you include those?)

Share this post


Link to post
Share on other sites

What happens currently when the expires_time is set to something other than 00:00:00? What are you expecting, and what are you getting? (If you are getting error messages, can you include those?)

 

 

Pls can Anyone help me with this code i am not getting it at all i have try but its not redirecting to header("Location: ". $MM_redirectLoginFailed ); when the user have 00:00:00 left in the database what can i do

 

$expires = '00:00:00';

 

//compare the expiry time with the current time

if ( $expires >= $LoginRS['expires_time'] )

{

header("Location: ". $MM_redirectLoginFailed );

 

}

else{

header("Location: " . $MM_redirectLoginSuccess );

}

Share this post


Link to post
Share on other sites

This is a guess off the top of my head, but I am betting that you are having issues because php is interpretting '00:00:00' as a string, not as time, which makes comparing the two difficult. You might try using seconds instead (so 1200 seconds is 20 minutes) which would allow you to compare a number against another number, rather than a string against a string?

Share this post


Link to post
Share on other sites

This is a guess off the top of my head, but I am betting that you are having issues because php is interpretting '00:00:00' as a string, not as time, which makes comparing the two difficult. You might try using seconds instead (so 1200 seconds is 20 minutes) which would allow you to compare a number against another number, rather than a string against a string?

 

May be there would be a better way for me to get what i really want because its the same as before when i use 1200 the result is still thesame when i set the value less than 1200 it work and when i set the value of the expires_time to be 60sec or 10 its not working as i want may be i am the one missing something . thank you very much Ben you are the best. i hope i can still get my result, still looking at the code and i have been searching on Google as well but have not found the right thing

Share this post


Link to post
Share on other sites

Are you sure you are getting valid data from the database. Have you tried echoing out $LoginRS['expires_time'] to make sure it is giving you the values you expect?

 

BTW, you could rewrite this section of code to be a bit more readable:

 

//compare the expiry time with the current time
if ( $LoginRS['expires_time'] <= 0 )
{
   header("Location: ". $MM_redirectLoginFailed );
}
else
{
   header("Location: " . $MM_redirectLoginSuccess );
} 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×