How to Set Cookies for Uuid

How to save uuid value for visitor

Make sure you check the cookie value before setting it, otherwise it will be overwritten every time and you won't know if the user's browser has already passed in an existing cookie. Other than that, this looks correct.

how to customize cookie in express?

You can set a cookie using res.cookie and node-uuid.

npm install node-uuid --save

var uuid = require('uuid');

Then in your middleware of your main application just check for a user object:

app.use(function(req, res, next) {
if (!req.user) {
res.cookie('uuid', uuid.v4());
}
next();
}

Assuming you have a user object on the req request object in the case of using express-session with passport.

Generating unique sessionId with Express Cookie-Session

Generating a unique identifier? That sounds like a job for universally unique identifiers- UUIDs! There's a lovely little Node.js package called uuid that can handle the logic behind them for you. Here's how you might use it to set a unique cookie in an ExpressJS application:

const express = require('express');
const uuid = require('uuid/v4');

const app = express();

app.get('/', (req, res) => {
if (req.cookie.id) {
return res.end(`Welcome back, ${req.cookie.id}!`);
}

const id = uuid();

res.cookie('id', id, { httpOnly: true });
res.end(`Welcome, ${id}!`);
});

app.listen(3000);

Your exact usage will probably be a little different, since you'd only need to generate a new UUID when somebody logs in, but the principles are the same.

P.S. - Have you considered the express-session package for identifying individual users of your application?

PHP Cookies and MySQL

You may need to look at setting a cookie in PHP correctly. the setcookie() function expects up to 6 parameters as follows:

setcookie(name,value,expire,path,domain,secure)

Your setcookie() has 7 parameters in the call and are not following the structure correctly:

setcookie("info", $uuid, $thisAid, $thisSubID, $thisURL, time()+30*24*60*60, "/")

In addition you are checking for an empty cookie named 'uuid' with your code empty($_COOKIE["uuid"]) however you are setting a cookie with the name info.

You would need to re-arrange this to be:

setcookie("uuid", $uuid, time()+30*24*60*60, "/")

However I don't know what the $thisAid, $thisSubID or $thisURL variables contain or are for?

W3 Schools have a pretty good example of setcookie() and there is always the PHP Manual itself

If you would want to use the one cookie to store all of your variables and then build your SQL query then you could either set a cookie seperately for each variable as so:

setcookie("uuid", $uuid, time()+30*24*60*60, "/")
setcookie("thisAid", $thisAid, time()+30*24*60*60, "/")
setcookie("thisSubID", $thisSubID, time()+30*24*60*60, "/")
setcookie("thisURL", $thisURL, time()+30*24*60*60, "/")
setcookie("campaignID", $thisCampaignID, time()+30*24*60*60, "/")

or alternatively set one cookie with a ; symbol to seperate each value as so:

setcookie("uuid", $thisAid . ";" . $thisSubId . ";" . $thisCampaignID. ";" . $thisURL . ";" . $uuid, time()+30*24*60*60, "/")

You could then use the explode() function to split the cookie value apart and then use a foreach loop on this to build your SQL query as below:

    $values = explode( ";", $_COOKIE['uuid'] );

foreach( $values as $value ) { $sqlValues .= $value . ","; }

$sql = "INSERT INTO WebTraffic (aid, subID, campaignID, URL, uuid) VALUES($sqlValues);

Alternatively using multiple setcookie() calls above you could do it as so:

    $sql = "INSERT INTO WebTraffic (aid, subID, campaignID, URL, uuid) VALUES($_COOKIE['thisAid'], $_COOKIE['subID'], $_COOKIE['campaignID'], $_COOKIE['thisURL'], $_COOKIE['uuid'] );

One other thing to note however is you really should be using prepared statements when inserting or retrieving information from a database to avoid SQL injection attacks. Do you know what these are/are you using any prevention techniques to avoid them?



Related Topics



Leave a reply



Submit