最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - create visitor unique ID? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Aug 27, 2012 at 5:17 rmtheis 5,82612 gold badges64 silver badges81 bronze badges asked Dec 8, 2009 at 9:47 jonesjones 6613 gold badges9 silver badges21 bronze badges 3
  • oh yeah i forgot to tell that in my site doesn't need resitered as a member... the unique visitor log file mean available for anonymous user. but how to identification unknown ip address and give the unique id? – jones Commented Dec 8, 2009 at 9:52
  • 1 Ever thought of just using cookies if you only need to store user specific configuration? – Tatu Ulmanen Commented Dec 8, 2009 at 10:22
  • Yes, i think using cookies that possible way... – jones Commented Dec 8, 2009 at 16:30
Add a ment  | 

4 Answers 4

Reset to default 10

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

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

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
}

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

发布评论

评论列表(0)

  1. 暂无评论