Jump to content

Archived

This topic is now archived and is closed to further replies.

Chips

Login fail messages for included sections

Recommended Posts

I have a page, where users can log in etc

The main page is called index.php, and within this page it has the login.php/loggedin.php - which is just a user login form, or the menu if they are already logged in. The below is a basic version of what I have (obviously more layout code on my proper version).

[code]
<?php //index.php
session_start();
?>

<table>
<tr>
<td> <?php
if($_SESSION['access']) {
include 'loggedin.php';
} else {
include 'login.php'
}
?></td>

<td><?php include '$page'; ?></td>

</tr></table>
[/code]


When a user logs in successfully, the "verify.php" will redirect them back to the index.php page again as follows:

header('location: index.php');

Obviously they will have had a session created for them, holding their access level inside it. When they get redirected, the $_Session['access'] will include the loggedin.php instead of login.php, so they see their user menu instead.

The problem is when someone tries to login and fails. How would I go about showing them an error message for why it failed? Right now I've just done it so that the redirect for failure is:
[code]header('location: index.php?error=1');[/code]
Obviously the value is 1 or 2 depending upon whether no such email exists in database, or if the password doesn't match the stored version.

When a user gets to the page I had added this:
[code]
<?php //index.php
session_start();

switch($_REQUEST['error']) {
case '1' : $content = "Login failed as no email exists";
break;

case '2' : $content = "Login failed as password doesn't match";
}
<table>
<tr>
<td> <?php
if($_SESSION['access']) {
include 'loggedin.php';
} else {
include 'login.php'
}
?></td>

<td><?php
if(isset($content)) {
echo $content;
} else {
include '$page';
}
?></td>

</tr></table>
[/code]

Now this will display the error message, but I am concerned that I am not handling this in the best way possible. The difficulty I am having is that my pages are based around a page with includes in it - for different content.
The include '$page'; is actually drawn from a database, where $page is the name of the file returned by a query dependant upon the id of said item - so that things like register forms can be brought up in the area. Does anyone have any suggestions or thoughts about my method? I'm trying to make it dynamic, so that one page can load lots of different cotent in it (for example, the links in logged in are actually index.php?id=2 in format, and id=2 is what content will result in being etc, or the register link si actually index.php?id=5 - where id 5 is actually the file named register.php, which is assigned to the $page value, and therefore included).

If this makes no sense, I'll try to clear it up further on request :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.