Jump to content

PHP problems


Recommended Posts

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 by Steve Scrimpshire
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Steve Scrimpshire
Link to comment
Share on other sites

'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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...