gilestodd Posted April 16, 2012 Share Posted April 16, 2012 I need help getting my login form to redirect to my admin area. I have been following tutorials on youtube trying to create a content management system and have made it as far as creating the form and creating the login action required to look for a username and password form my database and log in. I'll post up the pages I think are required for someone to give me some advice on where im going wrong. my login page login.php <html> <head> <title>Basic CMS - Admin Area - Login</title> </head> <body> <?PHP session_start(); if(isset($_SESSION['user'])) header("Location: index.php"); ?> <form action="dologin.php" method="post"> <table> <tr> <td><span>Username:</span></td> <td><input type="text" name="username" /></td> </tr> <tr> <td><span>Password:</span></td> <td><input type="password" name="password" /></td> </tr> <tr> <td colspan="2" align="right"><input type="submit" name="login" value="Login" /></td> </tr> </table> </form> </body> </html> my login actions page dologin.php <?php include('includes/functions.php'); session_start(); if (isset($_POST['login'])) { if(isset($_POST['username'])) { if(isset($_POST['password'])) { $username = $_POST['username']; $query = mysql_query("SELECT * FROM users WHERE Username = '$username'") or die (mysql_error()); $user = mysql_fetch_array($query); if(md5($_POST['password']) == $user['Password']) { echo "Login successful"; $_SESSION['user'] = $user['Username']; header("Location: index.php"); } else { echo "Please check your login details!"; include('login.php'); } } else { echo "Please check your password!"; include('login.php'); } } else { echo "Please check your username!"; include('login.php'); } } else { echo "Please check that you filled out the login form!"; include('login.php'); } ?> and my admin area index.php <?php session_start(); if(!isset($_SESSION['user'])) header("Location: admin/login.php"); ?> <html> <head> <title>Basic CMS - Admin Area</title> </head> <body> <span>Logged In! Welcome <?php> echo $_SESSION['user']; ?></span> <a href="logout.php">Logout</a> <a href="posts.php">Manage Posts</a> </body> </html> On logging in I am given "Login succesful" on the dologin.php page but I need it to redirect me to my index.php page, which is my admin area. If there's any other information you need to help me out just let me know. Any help anyone has for me is greatly appreciated! Thank you in advance. Quote Link to comment Share on other sites More sharing options...
NLT Posted April 16, 2012 Share Posted April 16, 2012 Can you not use header? <?PHP if(md5($_POST['password']) == $user['Password']) { $_SESSION['user'] = $user['Username']; header('Location: index.php'); // Redirect } ?> Quote Link to comment Share on other sites More sharing options...
Drummin Posted April 16, 2012 Share Posted April 16, 2012 Using headers and setting sessions must be done before anything is sent to the browser. Move all processing above <html> tag and don't echo anything. If needed, create variables in processing to be echoed in page body. So on dologin.php remove the "Login successful" echo. if(md5($_POST['password']) == $user['Password']) { $_SESSION['user'] = $user['Username']; header("Location: index.php"); Quote Link to comment Share on other sites More sharing options...
gilestodd Posted April 16, 2012 Author Share Posted April 16, 2012 I see, got that sorted, been succesfully redirected to my index.php page. However, im not veiwing any of my content. <html> <head> <title>Basic CMS - Admin Area</title> </head> <body> <?php session_start(); if(isset($_SESSION['user'])) header("Location: admin/login.php"); ?> <span>Logged In! Welcome <?php> echo $_SESSION['user']; ?></span> <a href="logout.php">Logout</a> <a href="posts.php">Manage Posts</a> <p>This is some test text...</p> </body> </html> Nothing is visable on screen, not even the test text I put in <p></p> tags, any advice on this one? Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Drummin Posted April 16, 2012 Share Posted April 16, 2012 Well that isn't right. Personally, I like to bracket my IF statements as well. I know others don't though <?php session_start(); if(isset($_SESSION['user'])){ header("Location: admin/login.php"); } ?> <html> <head> <title>Basic CMS - Admin Area</title> </head> <body> Quote Link to comment Share on other sites More sharing options...
gilestodd Posted April 16, 2012 Author Share Posted April 16, 2012 Thank you guys, got it sorted. Will probably hit some more issues when i develop the system further. Quote Link to comment 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.