Please help, I am writing a php login script. I really need help. I have tried it for days without success. I have this line of code on top of my login page
<?php
ob_start();
if (isset($_SESSION['admin'])) {
header('Location: admin.php');
}
Here is my login (index.php) script
<?php
require "includes/dc_conect.php";
if (isset($_POST['submit']))
{
$username=mysql_real_escape_string(htmlentities($_POST['username']));
$password=mysql_real_escape_string(htmlentities($_POST['password']));
if($username==NULL || $password==NULL)
{
echo 'All fields must be field';
}
else
{
$sql="SELECT * FROM users WHERE username='$username' && password='$password'";
$result=mysql_query($sql, $link);
$dbfield=mysql_fetch_assoc($result);
$count=mysql_num_rows($result);
if($count>0)
{
//Set username session variable
$_SESSION['admin'] = $username;
header('Location: admin.php');
}
else
{
echo"<blink>"."<font color='#FF0000'>"."Username and/or Password is incorrect!"."</blink>";
}
}
}
?>
when I am logging in, it returns back to the login page
here is the script on the top of my admin.php
<?php
// start session
ob_start();
session_start();
//check to see if user is already loged in den redirect
if(!isset($_SESSION['admin']))
{
header("Location: index.php");
exit();
}
else
{
require "includes/dc_conect.php";
$username=$_SESSION['admin'];
$sql="SELECT * FROM users WHERE username='$username'";
$result=mysql_query($sql, $link) or die (mysql_error());
$dbfield=mysql_fetch_assoc($result);
$count=mysql_num_rows($result);
echo $dbfield['username'];
}
?>
Please could someone help me?