Chips Posted June 16, 2006 Share Posted June 16, 2006 I have a page, where users can log in etcThe 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.phpsession_start();?><table><tr><td> <?phpif($_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.phpsession_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> <?phpif($_SESSION['access']) { include 'loggedin.php'; } else {include 'login.php' }?></td><td><?phpif(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 :) Quote Link to comment https://forums.phpfreaks.com/topic/12151-login-fail-messages-for-included-sections/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.