setting a cookie

This section is for posting questions which are not directly related to WYSIWYG Web Builder.
Examples of off topics: web server configuration, hosting, programming related questions, third party scripts.

Note that these questions will generally not be answered by the administrators of this forum.
User avatar
alex4orly
 
 
Posts: 530
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

setting a cookie

Post by alex4orly » Tue Jul 11, 2017 7:23 am

Hello all, I am trying to setup a cookie on the site for each visitor. Running the code below from within WWB in preview mode works fine. But when uploading the page to the site, it does NOT create the expected cookie. Any ide why not? I am using IE 11 (sorry about that...)

Cheers

<script type="text/javascript">

var navigator_info = window.navigator;
var screen_info = window.screen;
var uid = navigator_info.mimeTypes.length;
uid += navigator_info.userAgent.replace(/\D+/g, '');
uid += navigator_info.plugins.length;
uid += screen_info.height || '';
uid += screen_info.width || '';
uid += screen_info.pixelDepth || '';

setCookie(uid);

function setCookie(uid)
{
exdays = 365;
var cname = "visitors";
var d = new Date();

d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + uid + ";" + expires + "path=/";

alert(uid);
}
</script>

Hurtado
 
 
Posts: 691
Joined: Sun Nov 04, 2012 12:02 pm
Location: Valladolid
Contact:

Re: setting a cookie

Post by Hurtado » Tue Jul 11, 2017 6:34 pm

Use VueJS 2

User avatar
alex4orly
 
 
Posts: 530
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: setting a cookie

Post by alex4orly » Tue Jul 11, 2017 8:24 pm

Thanks for the tip - what is that?
Cheers

User avatar
alex4orly
 
 
Posts: 530
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: setting a cookie

Post by alex4orly » Tue Jul 11, 2017 9:14 pm

OK Hurtado,
I found out what it is, it seems to me an entire framework for something I thought will be very simple.
So, thanks but I can't go that way

Cheers

Hurtado
 
 
Posts: 691
Joined: Sun Nov 04, 2012 12:02 pm
Location: Valladolid
Contact:

Re: setting a cookie

Post by Hurtado » Wed Jul 12, 2017 9:54 am

VueJs is very light
And require only a few lines of code for cookies. Less of JS Vanilla

User avatar
alex4orly
 
 
Posts: 530
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: setting a cookie

Post by alex4orly » Wed Jul 12, 2017 10:42 am

Thanks for your comment.
I have in the meantime realized that the cookie was created properly, i just can't whete on my pc it is stored...
The command shell:cookies brings up a folder but the wrong one...
I am on windows 8.1 followed all google search suggestions but none works for me
I know the cookie is there because i am able to read the value an show it in an alert ()

Cheers

User avatar
BaconFries
 
 
Posts: 4429
Joined: Thu Aug 16, 2007 7:32 pm

Re: setting a cookie

Post by BaconFries » Wed Jul 12, 2017 11:06 am

Cookies can be found in "Temporary Internet Files' See the following:
http://www.whatarecookies.com/view.asp
"Select the 'Tools->Internet Options' menu item, and then click the 'Settings' button under the 'General' tab. Click the 'View Files' button to see a list of the temporary internet files that Explorer has stored. Cookies have names that begin 'cookie:' - the Internet Address column will tell you which website created the cookie.
Note - the 'Tools' option can also be accessed by clicking the 'gear' icon in Internet Explorer's toolbar."

User avatar
alex4orly
 
 
Posts: 530
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: setting a cookie

Post by alex4orly » Wed Jul 12, 2017 9:55 pm

Hi BaconFries,

Thanks for the guide, followed the steps but my cookies DOES NOT show up in the list.
I cleared all cookies from the list and started my test page again, but there is no cookie in the list
Yet, when I use getcookie, I have the value displayed on the page, this means that the cookie is there somewhere...

Any other suggestions?

Cheers

Hurtado
 
 
Posts: 691
Joined: Sun Nov 04, 2012 12:02 pm
Location: Valladolid
Contact:

Re: setting a cookie

Post by Hurtado » Sat Jul 15, 2017 12:32 pm


Hurtado
 
 
Posts: 691
Joined: Sun Nov 04, 2012 12:02 pm
Location: Valladolid
Contact:

Re: setting a cookie

Post by Hurtado » Sat Jul 15, 2017 12:56 pm

// Crear Cookie
var crearCookie = function (key, value) {
expires = new Date();
expires.setTime(expires.getTime() + 31536000000); // Estableces el tiempo de expiración, genius
cookie = key + "=" + value + ";expires=" + expires.toUTCString();
return document.cookie = cookie;
}

// Leer Cookie
var leerCookie = function (key) {
keyValue = document.cookie.match("(^|;) ?" + key + "=([^;]*)(;|$)");
if (keyValue) {
return keyValue[2];
} else {
return null;
}
}

// Eliminar Cookie
var eliminarCookie = function (key) {
return document.cookie = key + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

User avatar
alex4orly
 
 
Posts: 530
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: setting a cookie

Post by alex4orly » Sun Jul 16, 2017 10:09 am

Hello again,
Well, I sorted it out in PHP. The problem I had is - create a Cookie with a unique value so I can monitor the visitors in my village where we ALL have a central Internet provider and therefore we all show as ONE same IP address.
Initially I wrote the code into a separate page and tried to call it on the Onload of the Index.html page, but this was ignored and didn't work.
So, I renamed this PHP scrip to Index.php and at the end of it, call the front page of my website as you can see in the code below.
I am NOT happy with this arrangement, unless someone can give me another suggestion.

Cheers

<?php
if (session_id() == "")
{
session_start();
}

$firttime = 0;
$cookie_name = "beleura";

// This is 12 months span for cookie expiry
$cookie_value = (string)(time() + (86400 * 30 * 12));

// First time visitors, just set cookie
if(!isset($_COOKIE['beleura']))
{
setcookie($cookie_name, $cookie_value, time() + (86400 * 30 * 12), "/" );
$firttime = 1;
}

// either way, use the cookie value
// echo "Cookie '" . $cookie_name . "' is set!<br>";

// Function to get the client IP address
function get_client_ip()
{
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}

// Collect the required visitor's information
$today = date("Ymd H:i:s");

// Assign cookie value for the record
if($firttime == 0)
{
$cookievalue = $_COOKIE[$cookie_name];
}
else
{
$cookievalue = $cookie_value;
}

$visitorip = get_client_ip();
$details = json_decode(file_get_contents("http://ipinfo.io/{$visitorip}"));

$logdata = "\r\n";

// Construct the elemnts into one record
$output = $today . "," . $cookievalue . "," . $visitorip . "," . $details->country . $logdata ;
// echo $output; for testing only

// Write the record into the logfile
$fp = fopen("visitors.csv", "a");
fwrite($fp,$output);

fclose($fp);

// Now, just load the front page - this was originally the index.html page in this website
include ("frontpage.html");
?>

Hurtado
 
 
Posts: 691
Joined: Sun Nov 04, 2012 12:02 pm
Location: Valladolid
Contact:

Re: setting a cookie

Post by Hurtado » Wed Aug 02, 2017 1:04 pm

Can use, in Valnilla JS:

sessionStorage (scope: tab active)=>

Code: Select all

sessionStorage.setItem(clave,valor) or sessionStorage.clave (sessionStorage.User by sample)
sessionStorage.getItem(clave) or sessionStorage.clave (sessionStorage.User by sample)
localStorage (scope: browser)0>

Code: Select all

localStorage.setItem(clave,valor) or localStorage.clave (localStorage.User by sample)
localStorage.getItem(clave) or localStorage.clave (localStorage.User by sample)

User avatar
alex4orly
 
 
Posts: 530
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: setting a cookie

Post by alex4orly » Thu Aug 03, 2017 6:05 am

Thanks Hurtado,

Sorry, not sure what should I do with this, can you please explain?

Cheers

Hurtado
 
 
Posts: 691
Joined: Sun Nov 04, 2012 12:02 pm
Location: Valladolid
Contact:

Re: setting a cookie

Post by Hurtado » Mon Aug 07, 2017 4:12 pm

sessionStorage and localStorage is the new paradigma

Use is very simple, only create a JS function with previous code to use.

Full explain:

https://www.w3schools.com/html/html5_webstorage.asp

User avatar
alex4orly
 
 
Posts: 530
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: setting a cookie

Post by alex4orly » Mon Aug 07, 2017 9:46 pm

Thanks again,

I want to be able to identify in my retirement village which residents visit my website.
How can I get the machine name or some other identifier, setting surname=John is not an option for me, I don't know the name of the visitor

Cheers

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest