Ali you have over-complicated your code, keep it simple. I have rewritten the login.php page for you, but I have no knowledge about your “chat” database. You aren’t saving the the shouts in the database but trying to echo it out. Please explain how you are saving information in the chat database???
Here is the code that I’ve rewritten for you.
login.php
[php]
<?php
session_start();
require_once ('functions.php');
if (isset($_SESSION['email'])) {
header("Location: core.php");
}
?>
Login page
<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST' &&
!empty($_POST['username']) &&
!empty($_POST['password']) &&
isset($_POST['submit'])
) {
$conn = db_connect();
$username = safe_output($_POST['username']);
$password = safe_output($_POST['password']);
$result = query_usercheck($conn, $username, $password);
if (!is_bool($result)) {
$username = $result['email'];
$password = $result['password'];
$userid = $result['id'];
if ($password == 'admin') {
/* I have no idea about these scripts but I included it for your refernce, uncomment it if you want */
//include('admin.php');
// goto a;
}
$_SESSION['email'] = $username;
$_SESSION['id'] = $userid;
/* I'm assuming that core.php file is the file where you want users to post message, if thats the case. Just uncomment the header line and delete the include. */
header("Location: core.php");
// include('core.php');
} else {
echo "
Username / Password Invalid, Please Try again!
or if you are a new user please click
<a href=\"test.php\"> here</a> to register.</p>
";
}
}
?>
<div id="form">
<form action="" method="POST">
<p>
<label for='username'>Username: </label>
<input type="text" name="username" size="30" id="username" />
</p>
<p>
<label for='password'>Password: </label>
<input type="password" name="password" size="30" id="password" />
</p>
<p>
<input type="submit" name="submit" value="submit"/>
</p>
<form/>
</div>
<?php
if (isset($conn)) {
mysqli_close($conn);
}
?>
[/php]
functions.php
[php]<?php
function db_connect(){
/* Change the following according to your database connection variables */
$host = ‘localhost’;
$user = ‘tanzeelniazi’;
$pass = ‘abc’;
$db = ‘phphelp’;
$conn = mysqli_connect($host, $user, $pass, $db);
if (mysqli_connect_errno()){
die("Can't Connect: " . mysqli_connect_erro());
}
return $conn;
}
function safe_output($string){
$string = trim($string);
$string = strip_tags($string);
$string = htmlspecialchars($string);
return $string;
}
function query_usercheck($conn, $username, $password){
$query = "SELECT * FROM users
WHERE email = '{$username}' AND
password = '{$password}'
";
$results = mysqli_query($conn, $query);
if (!$results) {
die("Query Failed " . mysqli_error($conn));
}
if (mysqli_affected_rows($conn) == 0){
return FALSE;
}
$user = mysqli_fetch_assoc($results);
return $user;
}
?>[/php]