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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP 5.3.3 vs. PHP 5.4.16

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I have a PHP script which I'm using in a html form. It works fine in PHP 5.3.3, but not on 5.4.16.

<?PHP

$filename = "/tmp/restart.txt"; #Must CHMOD to 666
$text = $_POST['requester']; # Form must use POST. if it uses GET, use the line below:
$text2 = $_POST['reason']; # Form must use POST. if it uses GET, use the line below:

$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
$fp2 = fopen ($filename, "a"); # w = write to the file only, create file if it does not exist, discard existing contents

if ($fp) {
fwrite ($fp, "requester=");
fwrite ($fp2, $text);
fwrite ($fp2, "\nreason=");
fwrite ($fp2, $text2);
fclose ($fp);
sleep(1);
header("Location: }
else {
echo ("ERROR: Please check the status of the app.");
header("Location: }

?>


Form:

<FORM action="restart.php" method="post">
<label for="technician">Requester:</label>
<input type="text" id="requester" name="requester" />
&nbsp;
<label for="reason">Reason:</label>
<input type="text" id="reason" name="reason" />
<br><br>
<input type="submit" value="Restart App!">
</form>

Any thoughts on what the issue could be? I'm using the same HTML form on both servers.
 
Hi

You will have to give us details about how is not working. The code itself is syntactically correct and works fine for me in 5.6.30. Try to add [tt]error_reporting(E_ALL);[/tt] to the beginning of your script to make sure no message passes unnoticed. ( You may also need to set either [tt]display_errors[/tt] or [tt]log_errors[/tt]. )

Maybe the PHP version update replaced php.ini and now some of your old configuration is missing. For example [tt]open_basedir[/tt] is different, which may affect the [tt]fopen()[/tt]'s success.

Definitely no idea why are you opening the same file twice, but that is just weird, not a bug. But I would get rid of it as possible, just to be sure.

johngiggs said:
echo ("ERROR: Please check the status of the app.");
header("Location:
Unless you use output buffering, this will not work as [tt]header()[/tt] must be called before any actual content is sent out.


Feherke.
feherke.github.io
 
Hi feherke,

The only issue I'm having is that when I fill out the form on the HTML page it's not creating the .txt file.

All I'm attempting to do is to have the 2 form fields write to a text file. I have the code working fine on a few different machines, but for some reason in this case the file isn't being created.

I tried the error code snippet you mentioned, but it didn't seem to help.

Thanks,

John
 
Error reporting would help.

Code:
// enable error reporting to display in the browser
error_reporting(E_ALL);
ini_set('display_errors', 1);

Are you testing between 5.3.3 and 5.4.16 in the same environment? This smells like a simple issue of permissions for the particular version of PHP to write to your location.
 
THe first thing to do is to get errors showing. Comment out the redirections (header), otherwise the error would just go unseen when you change the page.





----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Hi,

I tried removing the header redirects, but I'm not seeing any logging to indicate what the problem is.

As I mentioned, I run the same php code and form on CentOS 5 and 6 with earlier versions of PHP and don't have any issues. This issue is on CentOS 7 with PHP 5.4.16.

Any other thoughts on what the issue could be? Was anything I posted deprecated in a newer version of PHP?

Thanks,

John
 
Hi,

Does anyone have any troubleshooting tips or ideas? It was a simple HTML form and PHP script, but it's no longer working on the new OS and PHP version.

If there's an alternate way to achieve the same thing I'd be open to doing it another way.

Thanks,

John
 
If this isn't working then there must be errors. Troubleshoot the error reporting first to troubleshoot the original problem.

Try creating a new PHP file to see if error reporting works. Then apply to your existing file.

Code:
<?php
// enable error reporting to display in the browser
// note that the following 2 lines are the very first functions in this script
error_reporting(E_ALL);
ini_set('display_errors', 1); 

// do something to prove an error
echo $anunsetvariable;
?>

To troubleshoot your existing script, it would look like this...

Code:
<?PHP
error_reporting(E_ALL);
ini_set('display_errors', 1); 

$filename = "/tmp/restart.txt"; #Must CHMOD to 666
$text = $_POST['requester']; # Form must use POST. if it uses GET, use the line below:
$text2 = $_POST['reason']; # Form must use POST. if it uses GET, use the line below:

$fp = fopen ($filename, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
$fp2 = fopen ($filename, "a"); # w = write to the file only, create file if it does not exist, discard existing contents

if ($fp) {
fwrite ($fp, "requester=");
fwrite ($fp2, $text);
fwrite ($fp2, "\nreason=");
fwrite ($fp2, $text2);
fclose ($fp);
sleep(1);
echo ("SUCCESS!");
//header("Location: [URL unfurl="true"]http://mysite.com/restart-complete.html");[/URL]
}
else {
echo ("ERROR: Please check the status of the app.");
//header("Location: [URL unfurl="true"]http://mysite.com/restart-error.html");[/URL]
}

?>
 
Hi,

I finally resolved the issue. I found that "PrivateTmp=true" is set in /usr/lib/systemd/system/httpd.service. I set it to false which resolved the issue.

systemctl daemon-reload
service httpd restart

Thanks,

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top