I'm using the script below for logging in. I'm trying to redirect the users who were succesfully logged in to another webpage (visible only for logged in users), but it redirects everyone - regardless if the username and password are correct. What's should be changed in the code?
page with login form - index.php
the page that I want to redirect to - content.php
Table in my database
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`emailadress` varchar(255) NOT NULL,
`pass` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
)
PHP
<?php
if (isset($_POST['username']) and isset($_POST['password'])){
$name = $_POST['username'];
$pass = $_POST['password'];
$query = "SELECT * FROM `person` WHERE name='$username' and pass='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
if ($count == 1){
$_SESSION['username'] = $username;
}else{
echo "Login failed.";
}
}
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
echo "Hello" . $username . "
";
echo "<a href='logout.php'>Logout</a>";}
?>
HTML
<form method="post" action="content.php" name="login">
<?php
if(isset($msg) & !empty($msg)){
echo $msg;
}
?>
<label for="username">Username:</label><br>
<input type="text" name="username"><br>
<label for="password">Password:</label><br>
<input type="password" name="password"><br>
<button type="submit" name="login">Log in</button>
</form>