Jump to content

[SOLVED] Keeping a cookie refreshed and/or active for substantial time?


chadrt

Recommended Posts

I have a dispatch system that I am building that on login it sets a cookie.  I would like to make that cookie refresh itself everytime a page is accessed.

 

Right now the cookie expires after 3600 seconds no matter what even if you are switching pages every 30 seconds.  The cookie also expires when the page is closed out.

 

first I want the cookie's time to be refreshed everytime the page is accessed and second I would like to see a check box on the login screen that will keep you logged for say 24hrs or a week.  Something so that I dont have to log in every hour.

Link to comment
Share on other sites

Okay this is just after a quick skim and a guess but

Add to top of index.php page

$hour = time() + 3600;
setcookie(ID_my_site, $_COOKIE[iD_my_site], $hour);
setcookie(Key_my_site, $_COOKIE[Key_my_site], $hour);

 

 

find code

$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour); 

 

replace with

<?php

$_POST['username'] = stripslashes($_POST['username']);
$x=3;
if(!empty($_POST['stay']))
{
switch($_POST['stay'])
{
	case "week":
		$x=(24*7);
	break;
	case "day":
		$x=24;
	break;
	default;
		$x=3;
	break;
}
}
$hour = time() + (60 * $x);
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour); 
?>

 

Now add a dropdown to the form called 'stay' and have the values "week" & "day"

 

that hopfully will do it

Link to comment
Share on other sites

Yes they would.. :D

 

kinda wrote in 2 parts lol..

okay if you update it to

session_start();
setcookie(ID_my_site, $_POST['username'], $_SESSION['cTime']);
setcookie(Key_my_site, $_POST['pass'], $_SESSION['cTime']); 

 

and login script.. update

<?php

$_POST['username'] = stripslashes($_POST['username']);
$x=3;
if(!empty($_POST['stay']))
{
   switch($_POST['stay'])
   {
      case "week":
         $x=(24*7);
      break;
      case "day":
         $x=24;
      break;
      default;
         $x=3;
      break;
   }
}
session_start(); //add
$cTime = time() + (60 * $x); //changed to cTime as hour seamed weird!
$_SESSION['cTime'] = $cTime; //Add
setcookie(ID_my_site, $_POST['username'], $cTime); //update
setcookie(Key_my_site, $_POST['pass'], $cTime);  //update
?>

Link to comment
Share on other sites

It appears as though the cookies are destroyed as soon as the browser is closed.  Is there a way to keep those there so that if you close out and return you are still logged in?

 

Thanks again btw,

Chad

The cookies normally aren't destroyed as soon as the browser is closed. However, if the browser settings are set to do so, they will be destroyed (example: Google's Chrome - Incognito mode). I'm going to guess in your case, that the time (6min) is too short.

 

This may be a bit off topic from this thread but do you see any significant problem with opening and closing php code to accomodate html?

It is actually faster to close php and do straight html, than to echo html inside of php. (less to parse)

Link to comment
Share on other sites

I guess I am back to the drawing board here as I cant seem to figure out why when I log in and then open the browser agian to a normally protected page it takes me back to the login screen.  Or for that matter if I open the login screen again it doesnt just dump be back to the main screens of my new dispatch system.

 

I dont normally use browse safe modes or incognito on my browsers and other sites keep me logged in just fine.  For example I use gmail and I am always logged in there.  And here on phpfreaks.com as well.  So there must be a problem with how the page is reading in the cookies when i navigate away from the page.

Link to comment
Share on other sites

I left the html portion of the page out if its needed just let me know.  This below is the login.php file.

<?php 

$action = $_GET['action'];
$landing = $_POST['landing'];

switch ($action)
{
case "missing":
	$error = "<font color=red>Please fill in both fields!</font>";
	break;
case "invalidu":
	$error = "<font color=red>Invalid Username!</font>";
	break;
case "invalidp":
	$error = "<font color=red>Invalid Password!</font>";
	break;
case "invalidc":
	$error = "<font color=red>Invalid Session!</font>";
	break;
case "logout":
	$error = "<font color=red>Logged out!</font>";
	break;	
default:
	$error = "Enter login info below.";
	break;
}
// Connects to your Database
mysql_connect("localhost", "*****", "*****") or die(mysql_error()); 
mysql_select_db("****") or die(mysql_error()); 

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{ 
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check )) 
{
if ($pass != $info['password']) 
{
}
else
{
header("Location: Dindex.php?page=dashboard");

}
}
}

//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted

// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
header("Location: login.php?action=missing");
}
// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
header("Location: login.php?action=invalidu");
}
while($info = mysql_fetch_array( $check )) 
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);

//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
header("Location: login.php?action=invalidp");
}
else 
{ 

// if login is ok then we add a cookie 
$_POST['username'] = stripslashes($_POST['username']);
$x=3;
if(!empty($_POST['stay']))
{
   switch($_POST['stay'])
   {
      case "week":
         $x=(24*7);
      break;
      case "day":
         $x=24;
      break;
      default;
         $x=3;
      break;
   }
}
session_start(); //add
$cTime = time() + (60 * $x); //changed to cTime as hour seamed weird!
$_SESSION['cTime'] = $cTime; //Add
setcookie(ID_my_site, $_POST['username'], $cTime); //update
setcookie(Key_my_site, $_POST['pass'], $cTime);  //update


// This sets the initial cookie!
//$hour = time() + 3600; 
//setcookie(ID_my_site, $_POST['username'], $hour); 
//setcookie(Key_my_site, $_POST['pass'], $hour); 

//then redirect them to the members area 
header("Location: Dindex.php?page=".$landing); 
} 
} 
} 
else 
{ 

// if they are not logged in 
?>

 

And here is the Dindex.php file.

 

<?php
session_start();
setcookie(ID_my_site, $_POST['username'], $_SESSION['cTime']);
setcookie(Key_my_site, $_POST['pass'], $_SESSION['cTime']);


$page = $_GET['page'];
$fileaccess9 = "allowRR783";

// Connects to your Database 
mysql_connect("localhost", "*****", "*****") or die(mysql_error()); 
mysql_select_db("*****") or die(mysql_error()); 

//checks cookies to make sure they are logged in 
if(isset($_COOKIE['ID_my_site'])) 
{ 
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['Key_my_site']; 
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 
while($info = mysql_fetch_array( $check )) 
{ 

//if the cookie has the wrong password, they are taken to the login page 
if ($pass != $info['password']) 
{ header("Location: login.php");
} 

//otherwise they are shown the admin area 
else 
{ 
// this creates the menu bar and header for the pages - content is static
include("headnav.php");

//main page content will change dynamically depending on which menu item is pressed.
include($page.".php");

//This page will include the copyright and any other information that will be at the bottom of pages statically.
include("footer.php");
} 
} 
} 
else 
//if the cookie does not exist, they are taken to the login screen 
{ 
header("Location: login.php"); 
} 
?>

 

Actually I am not logged in at all anymore.  It seems that I have to login every time I click a button within my system.  ouch...

Link to comment
Share on other sites

Ok I am not a PHP programmer by any means but I am getting better with each passing day.  Here is something that I think I found the code at the top of the Dindex.php

 

session_start();
setcookie(ID_my_site, $_POST['username'], $_SESSION['cTime']);
setcookie(Key_my_site, $_POST['pass'], $_SESSION['cTime']);

 

leads me to believe that it is looking for a username and password that have been posted to the page and in all actuallity I think the only way the page can really see a username and password is thru the session varriables not post.  So would I simply change the $_POST['username'],  to $_SESSION instead for the username and pass?

Link to comment
Share on other sites

Ok sorry to keep posting here I dont want anyone to think I am going crazy one post after another but I think I found a solution and if there is a better way then by all means please let me know!!

 

I added this to the top of the Dindex.php (logged in page of the dispatch sytem)

if(isset($_COOKIE['CT_my_site'])) 
{}else{
$hour = time() + 3600;
setcookie(ID_my_site, $_COOKIE[iD_my_site], $hour);
setcookie(Key_my_site, $_COOKIE[Key_my_site], $hour);
}

 

Then added this to the login page

if($_POST['stay'] ==""){}else{
setcookie(CT_my_site, $_POST['stay'], $cTime); //adding for use on the members page
}

 

(My goal is to be able to start relying on forums less and less and not make everyone else do the work for me, so I at least have to try to figure it out on my own!)

 

So far this seems to be working...

Link to comment
Share on other sites

Just for anyone following this thread the modifications that I posted did work however there was one problem.  The Day and Week calculations were actually calculating 24min as opposed to 24hrs. and the week was actually calculating 168min. to fix that I adjusted this:

 

$cTime = time() + (60 * $x); //changed to cTime as hour seamed weird!
to
$cTime = time() + (3600 * $x); //changed to cTime as hour seamed weird!

 

Many thanks to KingPhillip and MadTechie for all their help in this and I am very grateful to have a place like this to come and bounce my ideas off of and get help when needed.

 

~Chad

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.