Web Hosting Talk







View Full Version : PHP Login error...


Mblind21
11-30-2004, 04:41 PM
Posting this on behalf of a friend.




--------------------------------------------------------------------------------

quote:
--------------------------------------------------------------------------------
deagles :

<?php
session_start();
if($_SESSION['login'] != "valid")
{
header("Location:index.php");
}

?>
deagles : alright, when you login successfully $_SESSION['login'] = "valid"
deagles : so when someone who hasn't logged in tries to access the member's page, that script above should redirect them to the index page because $_SESSION['login'] was never set to valid
deagles : but it doesn't work
deagles : i keep getting this:


Notice: Undefined index: login in c:\inetpub\wwwroot\hades\members.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at c:\inetpub\wwwroot\hades\members.php:3) in c:\inetpub\wwwroot\hades\members.php on line 5

--------------------------------------------------------------------------------


This installation can be found at www.i-burn.net/hades/login.php

If you could reply to this and help me out, please post here! :D

Thanks in advance!

Vulture
11-30-2004, 05:27 PM
The first error is caused by the php error reporting being at maximum. If your friend has access to the php.ini file then the error_reporting field can be edited there. If you cannot access the php.ini file (if you don't think you can, you probably can't) then you can manually add:
error_reporting(E_ALL ^ E_NOTICE); to the code which will prevent the error from being shown. More Info (http://uk2.php.net/error_reporting)

The second error is caused because php is expecting header() to be sent before any output. I would suggest looking at output buffering (http://uk2.php.net/ob_start) which should help to correct this error.

Hope that helps :)

sonicgroup
11-30-2004, 05:46 PM
Rather than changing error reporting, check to see if the variable is empty before checking its value (this will get rid of the underfined index error).


<?php
session_start();
// if $_SESSION['login'] contains something
if (!empty($_SESSION['login'])) {
// if it's not "valid"
if($_SESSION['login'] != "valid") {
header("Location: index.php");
}
//otherwise, it's empty, you need to be kicked back anyway
header("Location: index.php");
}
?>