Display Error Message

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Display Error Message

Post 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
Hurklefish
Forum Newbie
Posts: 7
Joined: Sat Dec 20, 2003 4:03 pm

here's how I do it.

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Thanks for the tip Gen-iK! 8)
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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..
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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 ;)
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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..
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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 :)
Post Reply