Page 1 of 1

Display Error Message

Posted: Sun Dec 21, 2003 3:29 pm
by DuFF
Well I've been thinking for a while and still cannot come up with a good way to display my login/session errors.

Example: User tries to access a secure page without being logged in. I want to send him to the login page but I also want text above the login to say something like "You cannot access that page until you are logged in".

THe only way I can think of is by passing it in the URL (login.php?error=Please%20Login . . .) but I'd rather not have the error message displayed in the address bar. Does anyone have an idea of how to do this another way?

Thanks :D

here's how I do it.

Posted: Sun Dec 21, 2003 4:10 pm
by Hurklefish
On my login page, I have the form inputs lined up in a table. Above the inputs, I have a cell for warning messages, something like this..

Code: Select all

<?php

 echo "<TABLE border='0' cellpadding = '1' cellspacing = '1' bgcolor = '#cccccc'>"

 echo "<TR><TD colspan='2'>";
 $str_warn = $_GET['warn'];

if $str_warn==1
  {
  echo "Invalid login. Please check your user name and password and try again.";
}
echo "</TD></TR>";

echo "<TR><TD><INPUT type = 'text'........



?>
and so on.

In my actual code, I have an array of warning messages stored in a table, and I use that, or a switch statement on str_warn, to draw the appropriate message, or none at all.

Hope this helps.

Posted: Sun Dec 21, 2003 4:35 pm
by Gen-ik
Couldn't you add the error message to a session variable and then read it once you've bounced the user to another page.

SomePage.php

Code: Select all

<?php

session_start();

if($userNotSignedIn)
{
     $_SESSION["error"] = "Please log-in to access <b>".$_SERVER["PHP_SELF"]."</b>";

     header("Location:Login.php");
}

?>
Login.php

Code: Select all

<?php

session_start();

if(isset($_SESSION["error"]))
{
     echo $_SESSION["error"];
     unset($_SESSION["error"]);
}

?>
That's a similar way to how I normally store/display error messages etc.

Posted: Sun Dec 21, 2003 8:53 pm
by DuFF
Thanks for the tip Gen-iK! 8)

Posted: Sun Dec 21, 2003 9:02 pm
by d3ad1ysp0rk
hmmm good idea genik, never thought of that :p

I've always just sent them to login.php?x=1 or something and had

Code: Select all

if($x=="1"){
echo "You need to log in before viewing that page!<br>";
}
or something like that..

Posted: Sun Dec 21, 2003 9:47 pm
by Gen-ik
DuFF wrote:Thanks for the tip Gen-iK! 8)
No problem mate.

I've found that over the last few months I've been using $sessions more and more to store site data.. especially navigation and user/member info etc... it makes the info nice an easy to grab and it also keeps the address bar a bit more tidy.

I don't know how you use $sessions but I tend to create a single variable for say the member information as an object, a bit like...

$_SESSION["user"]->username;
$_SESSION["user"]->email;
$_SESSION["user"]->status;

...and so on.

Sessions are cool, I like 'em ;)

Posted: Sun Dec 21, 2003 10:00 pm
by d3ad1ysp0rk
does that make it any faster, or is it just for ease of thought?
cuz i usually use a whole new session var for everything..

Posted: Sun Dec 21, 2003 10:15 pm
by Gen-ik
I don't know to be honest, can't say that I've noticed any slowdown at all though.

A new site I'm working on loads the entire members profile and site settings into a single session object (like above) which is about 35 different variables from single numbers (int) through to the members biography which can be a big chunk of text (mysql longtext).

I guess it's quicker than constantly loading the info from a database, and once it's loaded you don't need to load it again.... unless the member changes their profile in which case you save the changes to the database and then reload the members info into the session.

And for myself it's also an ease-of-use and keeping-things-tidy way of working. Give it a whirl to try it out.. if you like it do it, if not don't :)