View Full Version : Check if cookie is set?
jenshansen
11-17-2015, 09:40 PM
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 :)
Beverleyh
11-17-2015, 10:15 PM
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-cookies-php-javascript
If you need further help, post your code and we can make suggestions.
jenshansen
11-19-2015, 10:42 AM
$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
Beverleyh
11-19-2015, 04:08 PM
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;
$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
}
jenshansen
11-20-2015, 10:08 AM
Alrighty.
Yeah i see that your code is a bit more compressed.
Thanks for your input and getting me in the right direction :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.