...

View Full Version : PHP cookie session_id manipulation



m444w
12-15-2010, 06:26 PM
I just started working with php cookies this week, and I want to offer a custom greeting that includes the last page they visited based on three scenarios:

Unique Visitor
Same session movement between pages
Returning visitor (with their last viewed page stored in their cookie)



<?php

if (isset($_COOKIE['websiteCookie'])){
$cookie=explode('[]',$_COOKIE['websiteCookie']); //allow manipulation of cookie vars
if (session_id() == $currentsession)
{

$last=$printme; //stores value that gets erased when new session is created
$eventhandler="I'm a returning user with a last page "; //testing variable


//creates a new session for returning user
$session = session_id();
$currentsession = session_id;
session_start();

//sets a new cookie
$cookie=".$last.'[]'.$sessionwebsite."; //Sets multiple settings within $cookie variable, must be exploded
$last=$_SERVER['HTTP_REFERER'];
$expire=time()+60*60*24*30; //Sets expire time
setcookie('websiteCookie', $cookie, $expire,'/','www.websitemfg.com');

}else{


echo($currentsession);
//handles users still on the website
$eventhandler="single session"; //testing variable
$last = $_SERVER['HTTP_REFERER'];




}

}else{


//Begins first session for new visitor

$currentsession = session_id(); //stores id of current session to determine if the user is new
session_start();
$eventhandler="unique user"; //testing variable
$last=$_SERVER['HTTP_REFERER'];




$cookie = $last.'[]'.$session; //Sets multiple settings within $cookie variable, must be exploded
$expire=time()+60*60*24*30; //Sets expire time
setcookie('websiteCookie', $cookie, $expire,'/','www.websitemfg.com');


}

?>



Currently I can get it to recognize a unique visitor, but it isn't handling the nested if after a person is already determined to have the cookie on their browser, and I'm not really sure why it isn't picking up the variable.

Thanks for the help!

m444w
12-15-2010, 07:56 PM
nevermind... Lunch break kickstarted my brain.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum