Jump to content

How to create the section on login forms which lets users log in forever ?


jd2007

Recommended Posts

I create my logins system with sessions too, but when you need the system to remember the user, than just stick to cookies.

 

You can create the cookie with the time limit you want:

 

setcookie('name', 'value', expire_time)

 

For one hour expire:

setcookie('name', 'value', time()+3600)

 

For ten days expire:

setcookie('name', 'value', time()+60*60*24*10)

 

You can set it to expire after 10 years (time()+60*60*24*365*10), so it practically is forever  ;D

 

 

Link to comment
Share on other sites

<?php
session_start();
header("Cache-control: private"); 

if ($_POST["login_username"]&&$_POST["login_password"])
{

  include("connect.php");
  $querya="SELECT confirm FROM users WHERE username='$_POST[login_username]' AND password='$_POST[login_password]'";
  $resulta=mysql_fetch_array(mysql_query($querya));
  if ($resulta['confirm']==1)
  {
  $queryb="SELECT * FROM users WHERE username='$_POST[login_username]' AND password='$_POST[login_password]'";
  $resultb=mysql_fetch_array(mysql_query($queryb));
  
  if($resultb>0){
    
    $_SESSION['Logged In']="true";
    $_SESSION['username']=$resultb['username'];
    $_SESSION['password']=$resultb['password'];
    $_SESSION['credits']=$resultb['credits'];
    $_SESSION['admin']=$resultb['admin'];
    header("location:index.php");
    }
  else {
    echo "Wrong username or password.";
  }
  }
  else 
  {
   echo "Please click <a href='resend.php'>here</a> to resend your confirmation mail."; echo $resultb; 
  }
}
else if (($_POST["login_username"]=="")&&$_POST["login_password"])
{
echo "Please enter your username.";
}
else if ($_POST["login_username"]&&($_POST["login_password"]==""))
{
echo "Please enter your password.";
}
else if (($_POST["login_username"]=="")&&($_POST["login_password"]==""))
{
echo "Please enter your username and password.";
}
else {}
?>

 

as u can see above, i use sessions, how to i modify the code above to switch to cookies...

Link to comment
Share on other sites

ok glad you found it, anyway for who doesnt know how to replace sessions with cookies in a case like this, just replace the $_SESSION['name'] = 'value' with setcookie('name', 'value', time()+3600). To request the value of a cookie, just use $_COOKIE['name']. And you dont need to use session_start() anymore.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.