Jump to content

Problems with include_once in html file


TrickyMoon

Recommended Posts

Hi

My webserver recently upgraded to php5/mysql5 and all of a sudden a counter script that I created stopped working...

 

Just before the header of my html file contains the @include_once function that use to execute my counter script everytime the html page is loaded. The url to the counter script contains 2 parameters (PHP_Self URL and the users ip address.) Once the counter script gets this information it updates or inserts a mysql table.

 

I can execute the counter script manually by cut/paste it into the browser, it updates the mysql table with no issues. For some reason however it does not do this in the html file through the include_once().  This has been working for the past 6-9 months and code hasn't changed.

Is there somethign different with @include_once in php/mysql 5? Any thoughts or recommendations on how to get this working again are greatly appreciated.

 

PHP code inside my html file. It includes the include_once() which should call the counter script every time the page is loaded.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<?php
	if (getenv(HTTP_X_FORWARDED_FOR)) {
		$ip   = getenv("HTTP_X_FORWARD_FOR");
		$host = gethostbyaddr($ip);
	} else {
		$ip   = getenv("REMOTE_ADDR");
		$host = gethostbyaddr($ip);
}
@include_once("http://www.mysite.com/counter.php?ip=$ip&url={$_SERVER[php_SELF]}");

/* just created this var for troubleshooting...the $curl returns the correct path and parameters. If I cut paste this exact variable when renedered in browser, Mysql table is updated w/o problems */

$curl = "http://www.mysite.com/counter.php?ip=$ip&url={$_SERVER[php_SELF]}";
echo "CounterURL: $curl";
?>
<head>
	<title> page title </title>
	<meta> etc....

	No more PHP code on page....

 

Here is my counter.php  again this has been working on php4 for the past 6-9 months w/o issues.

<?php
require_once('mysql_connect.php'); //connection to DB
$URL = $_GET['url']; //get url parameter from url that is passed through the @include_once()
$ip = $_GET['ip']; //get ip parameter from url that is passed through the @include_once()

//troubleshooting variables...when executing from browser these variables are returned correctly
echo "URL: $URL<br>";
echo "ip: $ip<br>";

$qip = "SELECT COUNT(url) as ct FROM ipaddr WHERE url='$URL' and date=CURDATE() and ip='$ip'";
$rip = @mysql_query($qip);
$rowip = @mysql_fetch_array($rip,MYSQL_ASSOC);

//if user ip address is NOT in db for curdate then add info to ipaddr table and counter table.
if($rowip['ct'] == 0) {
	$i1 = @mysql_query("Insert INTO ipaddr(url,ip,date) Values('$URL','$ip','CURDATE()')");
	$qurl = "SELECT url FROM counter WHERE url='$URL' and date=CURDATE()";
	$rurl = @mysql_query($qurl);

	if(@mysql_num_rows($rurl) == 0) {
		$query = @mysql_query("INSERT INTO counter(url,count,date) VALUES('$URL','1',CURDATE())");
	}
	else {
		$query = @mysql_query("UPDATE counter SET count=count+1 WHERE url='$URL'");

	}
}
?>

 

 

 

 

 

Link to comment
Share on other sites

Firstly, there are very few differences between PHP4 and PHP5 that prevent code from working.

 

99.5% of the time when code does not work between different versions of php or different servers, it is due to configuration differences and poor programming practices. Only about 0.5% of the time or less do actual changes in the php language cause code to not work.

 

Does your html file end in a .php extension or .htm/.html?

 

If it ends in .htm/.html, that would mean that the previous configuration parsed .htm/.html files as PHP but the current configuration does not.

 

You are suppressing errors in a number of places using the @ sign but your code has no error checking, error reporting, or error recovery logic of its own. That is just asking for broken and blank pages when something like the mysql server is down. Using @ also prevents the errors from getting written to the error log, so we cannot even suggest that you check the error log to help you find what the problem is.

 

Rather than suppress errors using the @, you should turn the display_errors setting off in a .htaccess file. But your code needs to also check the results returned by each function call and take appropriate action, such as output a meaningful message to the visitor and stop program execution, instead of blindly continuing execution when there might not be anything to execute on.

Link to comment
Share on other sites

ok, I've found out that PHP 5 has disabled URL based includes which I was using, I've taken this out of my include_once function and have substituted it with the following

 

<?php include_once($_SERVER['DOCUMENT_ROOT']."counter.php?ip=$ip&url={$_SERVER[php_SELF]}");?>

 

I've also removed the @ from the include_once to see what errors are being thrown. Below are my errors...the counter script is located in my directory root while the htm file that is calling it is located in another directory.

 

Not sure why it throws No Such file or directory since my include is calling something from another directory, not the directory where the htm file is. Also don't know much about the second inclusion error.

 

Error:

Warning: include_once(/home/mydomain/public_html/counter.php?ip=99.999.999.99&url=/mydir/mysubdir/mysubsubdir/index.htm) [function.include-once]: failed to open stream: No such file or directory in /home/mydomain/public_html/mydir/mysubdir/mysubsubdir/index.htm on line 20

 

Warning: include_once() [function.include]: Failed opening '/home/mydomain/public_html/counter.php?ip=99.999.999.99&url=/mydir/mysubdir/mysubsubdir/index.htm' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/mydomain/public_html/mydir/mysubdir/mysubsubdir/index.htm on line 20

 

Thanks in advance for any replies.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.