I have had look around stack overflow and a few other similar sites, and noticed there are a few people asking the same question as me but for them it works.
I'm new to php so excuse me if I have made some errors, my main problem with this code is that the IF statement which is deciding what page to go to seems to default to index.php. I have made a login table in MySQL already and have username/password column, and another column with a boolean value which states if the user is admin.
I have no idea why this doesn't work but hopefully I can get some help. Thanks for you
<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect(" ", " ", " ", " ");
// Selecting Database
$db = mysql_select_db(" ", $connection);
// SQL query to fetch information of registerd users and finds user match.
$query = "SELECT * FROM login WHERE username='$username' and password='$password'";
$result=mysql_query($query) or die(mysql_error());
$row= mysql_fetch_array($result);
$count=mysql_num_rows($result);
$auth=$row['admin'];
if($count==1){
if($auth['admin']==1){
session_start();
$_SESSION['admin']=$auth;
$_SESSION['username']=$username;
header("location: member.php");
}
elseif($auth['admin']==0){
session_start();
$_SESSION['admin']=$auth;
header("location:index.php");
}
} else {
$error = "Username or Password is invalid";
}
mysql_close($connection); // Closing Connection
}
}
?>
Aucun commentaire:
Enregistrer un commentaire