Forum Moderators: coopster

Message Too Old, No Replies

blank spaces

         

hal12b

9:49 am on Jun 5, 2008 (gmt 0)

10+ Year Member



I spent nearly an hour trying to figure this out as I am fairly new to PHP. The code below works fine if it starts on line 1. If it starts on any other line, it doesn't work. Can somebody explain to me why?

<?php
session_start();
$username = $_POST['USER'];
$passw = $_POST['PASSWORD'];
if ($username == "admin" && $passw=="pw"){
$_SESSION['SECURITY']=1;
header("Location: /phptest/admin/displayinfo.php");
}
else{
$_SESSION['SECURITY']=2;
header("Location: /phptest/admin/error.php");
}
?>

jatar_k

12:31 pm on Jun 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



do you get an error when you move it around? If so what is the error as that would probably tell the story.

deMorte

12:59 pm on Jun 5, 2008 (gmt 0)

10+ Year Member



I think session_start() needs to be the first in a script. Maybe this is why the script doesn't work when moved around.

jatar_k

1:20 pm on Jun 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I was wondering about the error because that is the most obvious part, the error would be something like 'headers already sent...'

the trick is though that you can't send any output to the browser before calling session_start, a blank line is output

if you are getting some other error then it's something else ;)

dreamcatcher

3:29 pm on Jun 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You also cannot use the header function if data has been sent to the browser. There are workarounds for this, but its good practice to understand how these functions work.

Always set your error reporting level to E_ALL for development. This can help with debugging and with learning.

dc

hal12b

5:32 pm on Jun 5, 2008 (gmt 0)

10+ Year Member



Yes, if I simply drop everything down one line, I get this error

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/jplwebde/public_html/bowfinancial/phptest/admin/login.php:2) in /home/jplwebde/public_html/bowfinancial/phptest/admin/login.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at /home/jplwebde/public_html/bowfinancial/phptest/admin/login.php:2) in /home/jplwebde/public_html/bowfinancial/phptest/admin/login.php on line 12

Dreamcatcher - where do you do this? --> Always set your error reporting level to E_ALL for development.

I am on a shared hosting environment. Is this something that can be set on a page or needs to be configured on the server?

Thanks

jatar_k

5:42 pm on Jun 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it can be set in php.ini

it can also be set per page using error_reporting() [php.net]

hal12b

6:18 pm on Jun 5, 2008 (gmt 0)

10+ Year Member



I am really new to this. Could you give me a code example to use?

g1smd

6:51 pm on Jun 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



[google.com...]

jatar_k

7:20 pm on Jun 5, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it shows it on the page I linked to

for a script you would put
error_reporting(E_ALL);

at the top

hal12b

8:26 pm on Jun 5, 2008 (gmt 0)

10+ Year Member



Thank you