Jump to content

[SOLVED] Keep User Logged In Forever


Northern Flame

Recommended Posts

I want to create an option on my members area

where the user can stay logged in forever,

I have never done this before but I came up with

an idea, can anyone let me know if I am starting

off good, and if I am not, can someone point me

in the right direction? heres my idea:

 

basically, if they want to stay logged on forever

i will just track there ip address on my database

 

<?php
/*
CHECK THE USERS LOGIN INFO HERE
*/

$forever = $_POST['forever']; //this is the checkbox the user checks if he wants to stay logged in
if(!empty($forever)){
$ip = $_SERVER['REMOTE_ADDR']; //users ip address
mysql_query("UPDATE users SET ip = '$ip' WHERE username='$username'");
}
?>

 

im guessing that would work fine, but my problem is when the user returns to my

website after already checking that he wants to stay logged in forever, how will

i check for this? I know I can check my database and then just establish the session

variables, but if each page is checking for the users return, each page will then have

to re-establish the session variables right? can something like this work:

 

<?php
if(!isset($_SESSION['id'])){
$ip = $_SERVER['REMOTE_ADDR'];
$check_login = mysql_query("SELECT * FROM users WHERE ip='$ip'");
if(mysql_num_rows($check_login) == 1){
// ESTABLISH SESSION VARIABLE
} else{
echo 'You are not logged in!';
}
}
?>

 

is that a smart way of doing this? or is there a smarter way?

Link to comment
https://forums.phpfreaks.com/topic/83128-solved-keep-user-logged-in-forever/
Share on other sites

and what about those people with a dynamic ip?

there is no way to truly keep your user logged in forever but the most common way is to set the cookie to expire eg 10 years later,

but this can still be gotten rid of when the user clears thier cookies,

 

 

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.