Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bad Code

Status
Not open for further replies.

Netwrkengeer

IS-IT--Management
Apr 4, 2001
184
US
I just wrote some code, and I'm getting an error

Warning: Failed opening '/usr/home/jbh/htdocs/codetest.phtml' for inclusion (include_path='.:/usr/local/lib/php') in /usr/home/jbh/htdocs/pagetest.phtml on line 9

Fatal error: Call to undefined function: codetest() in /usr/home/jbh/htdocs/pagepost.phtml on line 14

let me give you a description for the code,
This function (allegedly) takes an associative array and a URL. The array is URL-encoded and then POSTed to the URL. If the request succeeds, the response, if any, is returned in a scalar array. Outputting this is the caller's responsibility; bear in mind that it will include the HTTP headers. If the request fails, an associative array is returned with the elements 'errno' and 'errstr' corresponding to the error number and error message.

/*
this is the codetest.phtml code
*/
<?php

function codetest($DataStream, $URL) {

// Strip http:// from the URL if present
$URL = ereg_replace(&quot;^ &quot;&quot;, $URL);

// Separate into Host and URI
$Host = substr($URL, 0, strpos($URL, &quot;/&quot;));
$URI = strstr($URL, &quot;/&quot;);

// Form up the request body
$ReqBody = &quot;&quot;;
while (list($key, $val) = each($DataStream)) {
if ($ReqBody) $ReqBody.= &quot;&&quot;;
$ReqBody.= $key.&quot;=&quot;.urlencode($val);
}
$ContentLength = strlen($ReqBody);

// Generate the request header
$ReqHeader =
&quot;POST $URI HTTP/1.1\n&quot;.
&quot;Host: $Host\n&quot;.
&quot;User-Agent: codetest\n&quot;.
&quot;Content-Type: application/x- &quot;Content-Length: $ContentLength\n\n&quot;.
&quot;$ReqBody\n&quot;;

// Open the connection to the host
$socket = fsockopen($Host, 80, &$errno, &$errstr);
if (!$socket) {
$Result[&quot;errno&quot;] = $errno;
$Result[&quot;errstr&quot;] = $errstr;
return $Result;
}
$idx = 0;
fputs($socket, $ReqHeader);
while (!feof($socket)) {
$Result[$idx++] = fgets($socket, 128);
}
return $Result;
}
?>

/*
this is the pagetest.phtml code
*/

/*
this is Sample code calling codetest(). Note that it is better to test for an error result with isset() rather than simply if ($Result[&quot;errno&quot;]) as the error number can be zero.
*/

<?php

include(&quot;codetest.phtml&quot;);

$d[&quot;foo&quot;] = &quot;some&quot;;
$d[&quot;bar&quot;] = &quot;data&quot;;

$Result = codetest($d, &quot;
if (isset($Result[&quot;errno&quot;])) {
$errno = $Result[&quot;errno&quot;]; $errstr = $Result[&quot;errstr&quot;];
echo &quot;<B>Error $errno</B> $errstr&quot;;
exit;
}
else {
while (list($key, $val) = each($Result)) echo $val;
}
?>

/*
thanks Rob
*/
 
OK i changed the code to this
and now I get this error

Parse error: parse error in /usr/home/jbh/htdocs/pagetest.phtml on line 9
any ideas

/*
this is the codetest.phtml code
*/
<?php
/*
This function (allegedly) takes an associative array and a URL. The array is URL-encoded and then POSTed to the URL. If the request succeeds, the response, if any, is returned in a scalar array. Outputting this is the caller's responsibility; bear in mind that it will include the HTTP headers. If the request fails, an associative array is returned with the elements 'errno' and 'errstr' corresponding to the error number and error message.
*/

function codetest($DataStream, $URL) {

// Strip http:// from the URL if present
$URL = ereg_replace(&quot;^ &quot;&quot;, $URL);

// Separate into Host and URI
$Host = substr($URL, 0, strpos($URL, &quot;/&quot;));
$URI = strstr($URL, &quot;/&quot;);

// Form up the request body
$ReqBody = &quot;&quot;;
while (list($key, $val) = each($DataStream)) {
if ($ReqBody) $ReqBody.= &quot;&&quot;;
$ReqBody.= $key.&quot;=&quot;.urlencode($val);
}
$ContentLength = strlen($ReqBody);

// Generate the request header
$ReqHeader =
&quot;POST $URI HTTP/1.1\n&quot;.
&quot;Host: $Host\n&quot;.
&quot;User-Agent: codetest\n&quot;.
&quot;Content-Type: application/x- &quot;Content-Length: $ContentLength\n\n&quot;.
&quot;$ReqBody\n&quot;;

// Open the connection to the host
$socket = fsockopen($Host, 80, &$errno, &$errstr);
if (!$socket) {
$Result[&quot;errno&quot;] = $errno;
$Result[&quot;errstr&quot;] = $errstr;
return $Result;
}
$idx = 0;
fputs($socket, $ReqHeader);
while (!feof($socket)) {
$Result[$idx++] = fgets($socket, 128);
}
return $Result;
}
?>

/*
this is the pagetest.phtml code
*/


<?php
/*
this is Sample code calling codetest(). Note that it is better to test for an error result with isset() rather than simply if ($Result[&quot;errno&quot;]) as the error number can be zero.
*/

include(include_path=/usr/home/jbh/htdocs/codetest.phtml);

$d[&quot;foo&quot;] = &quot;some&quot;;
$d[&quot;bar&quot;] = &quot;data&quot;;

$Result = codetest($d, &quot;
if (isset($Result[&quot;errno&quot;])) {
$errno = $Result[&quot;errno&quot;]; $errstr = $Result[&quot;errstr&quot;];
echo &quot;<B>Error $errno</B> $errstr&quot;;
exit;
}
else {
while (list($key, $val) = each($Result)) echo $val;
}
?>

/*
thanks Rob
*/
 
As devnull says, verify that the codetest.phtml is set to chmod 644, or permissions
Code:
-rw-r--r--
so that it can be read by all users on the system. The Apache/PHP process runs as user &quot;nobody&quot; (usually), so perhaps your file is owned by the FTP user you use to upload the script, and is not world-readable (such as
Code:
-rw-------
).

Also, what text editor are you using to prepare this code, and on what operating system? Some of the less programmer-friendly text editors can include certain characters that are hidden in your windows or Mac environment, but cause trouble with your programming environment. Also, Unix is case-sensitive. Did you check to see if you accidentally capitalized the filename, or something like that?

Other than that, it's a problem that just plain shouldn't happen, so I think you're probably missing one of the very simple things. You can also just place all your code in one file, without the include, if you just want to test your form posting function.

As a test, have you tried calling codetest.phtml alone from the browser window. Put a small
Code:
echo &quot;Hello world.&quot;;
statement in there just as a test, and see if you can get that in the browser.
 
I'm running Php4 on FreeBsd and Apache, with a MySql db

codetest.phtml is set to chmod 644
text editor = windows notepad (what do you suggest to run on a windows platform)

did not accidentally capitalize any filenames

it works if I place all the code in one file without the include

and codetest does work alone in the browser

Ok the stuff I have read does not tell in detail any info on include statements I guess they never expect you to have a problem, ok what is the proper syntax for an include() statement, both normal and when calling from a different location.
Thanks
Robert
 
Proper syntax (without any further configuration settings in php.ini):

include(&quot;/absolute/path/to/file/filename.extension&quot;);
is the simplest and most assuring. PHP is good about figuring out whether you are putting a relative location to your include file or absolute. I always use absolute since it is 100% guaranteed to be successful.

Example:

the base location for your site is /usr/home/jbh/htdocs/ (also called document_root)

You have a file called pagetest.phtml that requires the use of codetest.phtml, so you must include it in pagetest.phtml. You have placed codetest.phtml in a folder called 'includes' within the location of your site (document_root). So, we do this:

include($DOCUMENT_ROOT.&quot;/include/codetest.phtml&quot;);
or even
include_once($DOCUMENT_ROOT.&quot;/include/codetest.phtml&quot;);

remember, $DOCUMENT_ROOT is a environment/system/global variable thus does not need to be set by you.

In your case, make sure that you are pointing to the right location where your included file resides. Also, if you are editing your scripts in Notepad or Wordpad in Windows, chances are, your files are being saved in a Windows binary format (with what they call ^Ms) These ^Ms in your code corrupt the code and you will have a lot of problems.

If you have access to telnet, SSH, or some other kind of terminal access to your account, login and open the file. If you see these ^M characters, you will know that you are in trouble (easy to clean up, though).

In any case, this is the general idea of includes.

Chad.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top