简体   繁体   中英

PHP cookie logging

I have 2 files on my host, first is c.php and second is log.txt. The file c.txt contains:

  <?php
  $cookie = $_COOKIE["name"]; 
  $steal = fopen("log.txt", "a");
  fwrite($steal, $name ."\n");
  fclose($steal);
  ?>

So it should log all the cookies and store it in log.txt file. But when I execute c.php, I don't get any errors, even then the log.txt file is empty.

The log.txt is fully writable and has all permissions (777). Please tell me what is going wrong. Thx in advanced.

Because you are not writing the cookie value.. Change $name to $cookie on your code.

The code...

<?php
$cookie = $_COOKIE["name"];
$steal = fopen("log.txt", "a");
fwrite($steal, $cookie ."\n"); //<---- Must be $cookie instead of $name
fclose($steal);
?>

or simply use a one-liner with the file_put_contents()

<?php
if(isset($_COOKIE["name"]))
{
    file_put_contents('log.txt',$_COOKIE["name"].PHP_EOL,FILE_APPEND);
}
else {  file_put_contents('log.txt',"No Cookie Found!!".PHP_EOL,FILE_APPEND); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM