Jump to content

PHP - please validate my 'test' code before I complain to hosting company


Recommended Posts

Hi

I have been developing an application in Windows, porting it to a Centos Linux server in-house, testing then porting it to a hosted environment which is generic linux. I've had no end of problems with the hosting company and getting my code to work. Right now, cookies and header dont seem to work so I wrote cookietest.php which calls cookietest2.php and should pass a cookie. I am asking you experts if you could validate my code before I get on the back of the hosting company. I dont profess to be a php expert but am baffled why my code works in WAMP and on Centos but not on a hosting company server!.

 

File 1: cookietest.php

<html>

<?php

  setcookie("test123","abc");

?>

<body>Hello..

You should not be able to read this because it will be too fast

 

<?php

  header("Location: testcookie2.php");

?></body>

</html>

 

 

File 2:

<html>

<body>

Hello

<?php

if(isset($_COOKIE['test123']))

{

  echo "cookie is set >>" .$_COOKIE ['test123'] ;

}

else echo "cookie not set";

 

 

?></body></html>

 

It runs find on my Centos box.

On the hosted companies, Header does not redirect, if I try echo'ing javascript href.location..... I can get to the second file but no cookie info.

When I try running php -version on the hosted system I am told there are missing libs:

 

[pow@cpanel6 ifp]$ php -version

Failed loading /usr/local/IonCube/ioncube_loader_lin_5.2.so:  /usr/local/IonCube/ioncube_loader_lin_5.2.so: cannot open shared object file: No such file or directory

 

 

On my Centos at home:

# php -version

PHP 5.1.6 (cli) (built: Sep 20 2007 10:04:27)

Copyright © 1997-2006 The PHP Group

Zend Engine v2.1.0, Copyright © 1998-2006 Zend Technologies

 

 

 

Many thanks in advance.

 

 

Kevin

this should NOT work, because you have output to the browser before the header() function is used. You should have NO output before header():

 

File 1: cookietest.php
<html>
<?php
  setcookie("test123","abc");
?>
<body>Hello..
You should not be able to read this because it will be too fast. Actually, you might be able to read this because the header() function should fail and leave this on the page with an error message like 'headers already sent.'

<?php
  header("Location: testcookie2.php");
?></body>
</html>

Cookies and headers MUST be sent before ANY output. Have you errors turned off or something? Your code should look like this:

 

testcookie.php

<?php
setcookie('test123', 'abc');
header('Location: testcookie2.php');
?>

 

testcookie2.php

<?php
if (isset($_COOKIE['test123'])) {
echo "Cookie 'test123' is set to {$_COOKIE['test123']}";
} else {
echo 'Cookie not set';
}
?>

Have you errors turned off or something?

 

good call. maybe it's "working" locally because errors are turned off and not working on the host server because errors are not turned off. it technically shouldn't be working. yes, cookies before output also, as they are also header content.

To OP:

Maybe you got this right, but remember to set the expire, path and domain parameters in setcookie(), to control when the cookie expires, in which directories the cookie is available and on which (sub)domains it's available, respectively.

The code should not work at all no matter whatever error_reporting or display_errors is set to. The only way the code will work is if the OP has a setting called output_buffering set to On within the php.ini for thier WAMP setup, quote from the php.ini

; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit.  You can enable output buffering during runtime by calling the output
; buffering functions.  You can also enable output buffering for all files by
; setting this directive to On.  If you wish to limit the size of the buffer
; to a certain size - you can use a maximum number of bytes instead of 'On', as
; a value for this directive (e.g., output_buffering=4096).

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.