Forum Moderators: coopster

Message Too Old, No Replies

Trouble with PHP headers

php headers redirect problems notice warning

         

historykat

3:31 am on Aug 29, 2011 (gmt 0)

10+ Year Member



I keep getting the same two problems no matter what I do ...

Notice: Undefined index: name in /Users/KatKat/localhost/cms/_admin/admin.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at /Users/KatKat/localhost/cms/_admin/admin.php:4) in /Users/KatKat/localhost/cms/_admin/admin.php on line 6

I have looked through tons of manuals and posts and I can't seem to find a solution that fixes the problems and still does what I need. The PHP on the page is ...

<?php
session_start();

if (!$_SESSION['name'])
{
header("Location: ../_scripts/login.php");

} else {
include ('admin_cont.php');
}

?>

The include is tiny too ...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

echo "Welcome, " . $_SESSION['name'] . "! <br /><a href='../_scripts/log_out.php'>Click here to Log out.</a>";

?>
</body>
</html>

That's all she wrote ...

Thanks

g1smd

7:52 am on Aug 29, 2011 (gmt 0)

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



Ouch. The HEADER directive is malformed.

You MUST include domain name and full path, when you use HEADER. Partial paths, and especially those beginning ../ are not allowed.

Additionally, you're sending a 302 redirect back to the browser, making the browser send a new request to fetch a different file, rather than the server just sending it for the current request.

Shouldn't you be using an include here too?

historykat

8:51 am on Aug 29, 2011 (gmt 0)

10+ Year Member



Thank you! I have been trying to frankenstein a page based on four different tuts ... never sure what can go together.
By using another include do you mean doing the if no session variable then include the file instead of redirecting?
Sorry if that is a totally daft question - I am way over my head.

penders

9:44 am on Aug 29, 2011 (gmt 0)

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



Also, to avoid your "Notice: Undefined index: name in ..."

if (!$_SESSION['name'])


Should perhaps be:
if (empty($_SESSION['name']))

rocknbil

3:43 pm on Aug 29, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd say isset, not empty, if it's not set it will still generate the error . . . right?

if (! isset($_SESSION['name']) or (isset($_SESSION['name']) and empty($_SESSION['name']))) {
}

g1smd

5:30 pm on Aug 29, 2011 (gmt 0)

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



Yes, use include instead of header, and ISSET is likely the right thing to use too.

historykat

7:34 pm on Aug 29, 2011 (gmt 0)

10+ Year Member



Thank You!

penders

9:14 pm on Aug 29, 2011 (gmt 0)

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



I did wonder about using !isset() rather than empty(), but suggested empty() in this case for the following reasons:

- Negating the expression is usually best avoided as it can make it harder to read, so in this case if you were going to use isset() you'd reverse the logic and say... if (isset($_SESSION['name'])) {include 'admin_cont.php';} else {...}

- empty() does not raise an E_NOTICE if the variable is not set, it just returns (bool)true. You don't need to check that a variable isset() before checking that it is empty().

- empty() more closely matches the current logic. (!$_SESSION['name']) is true on empty string, 0 and '0', the same as empty(). Changing to using isset() might break the current script if $_SESSION['name'] is not being explicitly unset() or the session destroyed.