jones
jones

Reputation: 661

create visitor unique ID?

I plan to create visitor unique ID and named as log file, as existing now I use the IP visitor as log file name i.e. logs/127.0.0.1.php but I think this is not enough because some visitor using share an IP address for PC's.

The visitor log file itself as setting place of configuration of visitors itself, so I plan to add another unique ID to identify each different visitor so let's say the log file: logs/127.0.0.0.1-t3451dq.php, -t3451dq as unique ID so as long as visitor browsing on my website the unique log file as setting configuration for each user (because I use plain text)

Currently I use:

<?
$filename = "./logs/".$_SERVER['REMOTE_ADDR'].".php" ; //out put logs/127.0.0.1.php
$data stripcslashes($data);
// each Visitor configuration here...
// bla...bla...

/* Writing file configurations */
    $buat = fopen($filename, "w+");
    fwrite($buat, "$data");
    fclose($buat);
?>

so I need $filename add the $unique ID as name of their log file. Any ideas how to do that?

Upvotes: 5

Views: 15558

Answers (4)

Alex
Alex

Reputation: 1609

using log files for this type of use is unnecessary, it's alot easier to just shunt this type of data to a database. If it's just temporary data then use Cookies and/or Sessions

Upvotes: 0

Arkh
Arkh

Reputation: 8459

You have two simple options : uniqid or as you're creating a file tempnam

Tempnam example :

function log($string, $userIP = null, $filename = null){
  // Check if filename exists
  if(!file_exists(LOG_PATH.$filename)){
    $filename = tempname(LOG_PATH, $userIP.' - ');
    if(!$filename){
      return false;
    }
  }
  // write log into file
  $file = file_put_contents($filename, $string);
  if($file === false || $file != strlen($string)){
    return false;
  }
  return $filename
}

Upvotes: 0

row1
row1

Reputation: 5578

Try uniqid.

You can store this unique ID in the users session or in a cookie.

Example (not tested)

session_start();
if(!isset($_SESSION['uniqueID']))
{
    $_SESSION['uniqueID'] = uniqid();
}
$filename = "./logs/".$_SESSION['uniqueID'].$_SERVER['REMOTE_ADDR'].".php" ;

Using a session will mean that if the same user closes their browser (or the session expires) they will get a new ID, which may or may not be what you want.

If you want a more persistent tracker then you may be better using cookies, and store the ID in the cookie (create a new ID if no cookie exists).

if(!isset($_COOKIE['uniqueID']))
{
    $expire=time()+60*60*24*30;//however long you want
    setcookie('uniqueID', uniqid(), $expire);
}
$filename = "./logs/".$_COOKIE['uniqueID'].$_SERVER['REMOTE_ADDR'].".php" ;

If you cannot use cookies/session then you may need to pass around the ID in your URL query string e.g. mypage.php?id=35dfgdfg3434

Upvotes: 9

TheHippo
TheHippo

Reputation: 63139

Create something out of his IP and the first time he enters the page. That should be unique.

Upvotes: 0

Related Questions