Results 1 to 5 of 5

Thread: Check if cookie is set?

  1. #1
    Join Date
    Nov 2015
    Location
    Denmark
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Check if cookie is set?

    Hi guys

    I wanna place a cookie which stores info about which url is first seen when entering my site.

    So i wanna create a cookie when a new user ends and set it for 1 day or so. So my question is:

    1. How can i set a php cookie with the value of first seen url

    2. How can i check when the user navigates others pages if the cookie is already set (so that i wont overwrite it.)

    Thanks in advance, very much appreciated

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Maybe you could try this logic;

    Capture referring URL with $_SERVER['HTTP_REFERER']

    Check if URL contains your domain with strpos() http://php.net/manual/en/function.strpos.php

    If it does contain your domain, check for existence of cookie

    If cookie doesn't exist, write URL to new cookie.

    You might already have your own preferences for setting and getting / reading and writing cookies but if you don't, here's one that came up high on Google http://www.pontikis.net/blog/create-...php-javascript

    If you need further help, post your code and we can make suggestions.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Nov 2015
    Location
    Denmark
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Code:
    $current_page = $_SERVER['REQUEST_URI'];
    $current_page2 = str_replace("/", "", $current_page); 
    
    if ($current_page == '/')
    {
    $current_page_out = "frontpage";	
    }
    else if (strpos($current_page,'gclid') !== false) 
    {
      $current_page_out = "adwords";	
    }
    else
    { 
    $current_page_out = $current_page2;
    }
     
    if( isset( $_COOKIE['first_page']  ) ) 
    { 
        
    } 
    else 
    { 
    $cookie_name = "first_page";
    $cookie_value = "{$current_page_out}";
    setcookie($cookie_name, $cookie_value, time() + (86400), "/"); // 86400 = 1 day
    }
    I think this solved my problem for https://moneybanker.dk.

    At least im trying that for now, to see if it works. Do you have any ideas to code it better ?

    Best regards,
    Jens Hansen
    Last edited by Beverleyh; 11-19-2015 at 12:35 PM. Reason: formatting

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Not really - if its working for you then great.

    I'd maybe try to reduce the code a bit, but that's just formatting preference;
    Code:
    $current_page = $_SERVER['REQUEST_URI'];
    $current_page2 = str_replace("/", "", $current_page); 
    
    if ($current_page == '/') {
    	$current_page_out = "frontpage";	
    	} else if (strpos($current_page,'gclid') !== false) {
    	$current_page_out = "adwords";	
    	} else { 
    	$current_page_out = $current_page2;
    	}
     
    if (!isset($_COOKIE['first_page'])) { 
    	$cookie_name = "first_page";
    	$cookie_value = "{$current_page_out}";
    	setcookie($cookie_name, $cookie_value, time() + (86400), "/"); // 86400 = 1 day
    	}
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. The Following User Says Thank You to Beverleyh For This Useful Post:

    jenshansen (11-20-2015)

  6. #5
    Join Date
    Nov 2015
    Location
    Denmark
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Alrighty.

    Yeah i see that your code is a bit more compressed.

    Thanks for your input and getting me in the right direction

Similar Threads

  1. Resolved quick question about COOKIE check
    By ?foru in forum PHP
    Replies: 12
    Last Post: 03-14-2009, 07:38 PM
  2. Changing from session cookie to permanent cookie
    By boisemedia in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-21-2008, 10:07 PM
  3. Replies: 4
    Last Post: 12-04-2007, 10:52 PM
  4. Can't set Cookie in IE6
    By sdevic in forum JavaScript
    Replies: 4
    Last Post: 08-17-2007, 03:51 PM
  5. Replies: 4
    Last Post: 12-18-2006, 12:23 PM

Bookmarks

Posting Permissions

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