Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP cookie session_id manipulation

    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)

    Code:
    <?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!

  • #2
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nevermind... Lunch break kickstarted my brain.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •