Nathan
Nathan

Reputation: 12000

Displaying error messages with PHP

I want to display an error if a user tries to go to /admin/ when they aren't logged in as an admin. I don't want to pass stuff in the URL and I do not want to do a $_POST to display the error. I just want to display a message and when you refresh it is gone.

For example, go to this URL: http://getsatisfaction.com/getsatisfaction/topics/notifications_box/edit

It returns you back to the topic and says "I'm sorry, but you have been denied access to edit this topic."

When you refresh it is gone. I want to be able to display an error like that too. Does anyone know how they did that?

I've seen other sites do this as well (without appending an ?error=1 to the end of the URL).

Thanks in advance.

Upvotes: 1

Views: 1088

Answers (2)

Nir Alfasi
Nir Alfasi

Reputation: 53525

You need to check the session and see if the user has permission. Checking the session is specific to the environment, for example, if you use Joomla: http://www.howtojoomla.net/how-tos/development/how-to-use-sessions-in-joomla

and if you use drupal: http://drupal.org/node/360542

of course that there's a native library for sessions in php: http://php.net/manual/en/ref.session.php

hope it helps!

Upvotes: 1

deceze
deceze

Reputation: 521994

Set the error message in the session:

session_start();
$_SESSION['message'] = 'No, you fool!';
header('Location: some-other-page.html');
exit;

Display the message:

session_start();
if (!empty($_SESSION['message'])) {
    echo $_SESSION['message'];
    unset($_SESSION['message']);
}

Upvotes: 2

Related Questions