Jump to content

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 :)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.