Steve Scrimpshire Posted February 22, 2005 Report Share Posted February 22, 2005 (edited) Why is $_SERVER['HTTP_REFERER'] suddenly giving me an invalid index error? [root@desktop html]# rpm -qa | grep php php5-posix-5.0.2-1mdk php5-xml-5.0.2-1mdk php5-dbase-5.0.2-1mdk php-xmlrpc-4.3.8-1mdk libphp_common432-4.3.8-3mdk php5-ini-5.0.2-1mdk libphp5_common5-5.0.2-1mdk php5-dba-5.0.2-1mdk php5-exif-5.0.2-1mdk php5-ftp-5.0.2-1mdk php5-ctype-5.0.2-1mdk php5-cli-5.0.2-1mdk php-ini-4.3.8-1mdk apache2-mod_php5-2.0.50_5.0.2-1mdk php-cli-4.3.8-3mdk php-pear-HTML_Common-1.2.1-3mdk php5-sysvsem-5.0.2-1mdk libc-client-php0-2004-1mdk php-manual-en-4.3.8-1mdk php5-gd-5.0.2-1mdk php5-pcre-5.0.2-1mdk php5-session-5.0.2-1mdk php5-mysql-5.0.2-1mdk php5-ming-5.0.2-1mdk php5-fileinfo-5.0.2_0.2-1mdk php-xml-4.3.8-1mdk php-pear-4.3.8-1mdk php5-recode-5.0.2-1mdk php5-gettext-5.0.2-1mdk php5-sysvshm-5.0.2-1mdk php5-filepro-5.0.2-1mdk php-cgi-4.3.8-3mdk php5-dbx-5.0.2-1mdk php5-yp-5.0.2-1mdk php5-cgi-5.0.2-1mdk Any ideas? It seemed to work yesterday and I haven't updated it recently. Maybe I just missed it. Edited February 23, 2005 by Steve Scrimpshire Quote Link to comment Share on other sites More sharing options...
theYinYeti Posted February 22, 2005 Report Share Posted February 22, 2005 The HTTP_REFERER is absent if not present :) Let me explain: You'd expect $_SERVER['HTTP_REFERER'] to return null or '' if this HTTP header is not present. Instead, the 'HTTP_REFERER' key is not created at all! So you should use array_key_exists() before, or use @$_SERVER[...]. Now why is this key absent? Only you can tell. It is normal if you enter the URL in the URL field of your browser because HTTP_REFERER should only exist if you came to this page through a hypertext link. But even if you use a link, the referer might be absent because of the browser's configuration. Many modern browsers allow for hiding the referer for better privacy. Yves. Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 22, 2005 Author Report Share Posted February 22, 2005 (edited) Thanks for the info. A little more detail on my problem (I was tired/frustrated when I wrote that). I have these scripts that I put in my favorite topics as *.png files here so that I can count the number of views (I have the *.png set up to be executed as php on my server). Up until yesterday or a couple days ago, the $_SERVER['HTTP_REFERER'] contained the url to the topic the image was actually in. Now it doesn't and errors out. Here's the code: <?php $refer = $_SERVER['HTTP_REFERER']; // Thanks, paul $Array = array(10447 => array("counter.txt","ipaddresses","RepairScript"), 22837 => array("counter2.txt","ipaddresses2","SetiStats")); $string=explode("showtopic=", $refer); $count=(count($string))-1; $string=explode("&", $string[$count]); $TopicNum=$string[0]; $counter_file = $Array["$TopicNum"][0]; $ipaddresses = $Array["$TopicNum"][1]; $script = $Array["$TopicNum"][2]; $submitted_email="me@wherever.com"; ini_set(sendmail_from, $submitted_email); if (getenv("HTTP_CLIENT_IP")) { $ip = getenv("HTTP_CLIENT_IP"); } else if (getenv("HTTP_X_FORWARDED_FOR")) { $ip = getenv("HTTP_X_FORWARDED_FOR"); } else if(getenv("REMOTE_ADDR")) { $ip = getenv("REMOTE_ADDR"); } else { $ip = "UNKNOWN"; } $isthere = "0"; $somecontent = "$ip\n"; $filestuff = file("$ipaddresses"); foreach ($filestuff as $line_num => $line) { $line = trim($line); $ip = trim($ip); if ($line === $ip) { $isthere = 1; } } if ($isthere == "0"){ if (is_writable($ipaddresses)) { if (!$handle = fopen($ipaddresses, 'a')) { echo "Cannot open file ($ipaddresses)"; exit; } if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } } // Open the file for reading if (!($fp = fopen($counter_file, "r"))) die ("Cannot Open Counter file: $counter_file."); // Read 20 characters from the file $counter = (int) fread($fp, 20); // Close the file fclose($fp); // Increment the counter $counter++; // Open the file, in write mode $fp = fopen($counter_file, "w"); // Write the new value of counter to the file. fwrite($fp, $counter); // Close the text file. fclose($fp); $hostname = gethostbyaddr($ip); mail("$submitted_email", "Someone viewed $script", "IP address: $ip\nHostname: $hostname\nIt has now been viewed $counter times. Cool!\n","From: $submitted_email") or die("error"); } ini_restore(sendmail_from); Header ("Content-type: image/png"); Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT"); Header("Pragma: no-cache"); $img = @ImageCreatefromPNG("blank.png"); ImagePNG($img); ImageDestroy($img); ?> Now, I can create a different script for each topic, but I was really enjoying being able to use the HTTP_REFERER and keep just one script. Is there another way I can capture the URL of the topic the image is posted in? Edited February 22, 2005 by Steve Scrimpshire Quote Link to comment Share on other sites More sharing options...
paul Posted February 22, 2005 Report Share Posted February 22, 2005 can you post the output of <? phpinfo() ?> please :) Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 22, 2005 Author Report Share Posted February 22, 2005 Sure http://omarserenity.no-ip.com/phpinfo.php Thanks. Quote Link to comment Share on other sites More sharing options...
paul Posted February 23, 2005 Report Share Posted February 23, 2005 'HTTP_REFERER' The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted. although I can see REFERER in your phpinfo. I wonder if you could hack a script that has img src="myscript.php?ref=190566" which you could place in each of your tips. then you could drop the $_SERVER vars and explode function, and just use $_GET[ref] just a thought :) Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 23, 2005 Author Report Share Posted February 23, 2005 It is a thought, but the board will only let me use img tags around things that end in jpg jpeg png or gif :P Quote Link to comment Share on other sites More sharing options...
paul Posted February 23, 2005 Report Share Posted February 23, 2005 mkdir MyFunkyScript mv myscript.php MyFunkyScript/myscript.png cd MyFunkyScript vi .htaccess ForceType application/x-httpd-php :wq Just another thought :) Quote Link to comment Share on other sites More sharing options...
paul Posted February 23, 2005 Report Share Posted February 23, 2005 or myscript.php?ref=190655&ext=.png Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 23, 2005 Author Report Share Posted February 23, 2005 mkdir MyFunkyScript mv myscript.php MyFunkyScript/myscript.png cd MyFunkyScript vi .htaccess ForceType application/x-httpd-php :wq This is how I'm doing it already, but it doesn't allow the passing of parameters, I'll try the last one....looks promising. Thanks for all the suggestions. Quote Link to comment Share on other sites More sharing options...
paul Posted February 23, 2005 Report Share Posted February 23, 2005 you could Error 404 myscript.php (can't remember the exact syntax) in your .htaccess then call 190543.myscript.php then strip the topic num from $_SERVER[php_SELF] just making suggestions ;) Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 23, 2005 Author Report Share Posted February 23, 2005 I thought that was gonna be it, but it didn't work. I think I'll try your &ext=.png trick next when I get a chance. Thanks again for all your suggestions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.