Jump to content

Login Form Redirection


gilestodd

Recommended Posts

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.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.