I have an issue with the header() and when I use my login script it redirects it back to the index.php page. I have looked at other posts on here and none of their methods worked for me, so I was wondering if i could get input from you guys for my situation. Here is my login script.
<?php
require_once("database.php");
if(isset($_POST['submit']))
{
$myusername = mysqli_real_escape_string($db,$_POST['myusername']);
$mypassword = mysqli_real_escape_string($db,$_POST['mypassword']);
$mem = mysqli_query($db,"SELECT * FROM members WHERE username='$myusername' AND password='$mypassword'");
$info=mysqli_fetch_assoc($mem);
$count=mysqli_num_rows($mem);
if($count==1){
session_start();
$_SESSION['MyID'] = $info['id'];
$_SESSION['MyUsername'] = $info['username'];
header("Location:home.php");
exit;
}else{
echo '
<td colspan="3">
<table cellpadding="1" align="center" bgcolor="#cccc99"><tr><td>
<table cellpadding="2" bgcolor="#FFFFCC"><tr><td>Invalid username or password. Please re-enter your user information. </td></tr></table>
</td></tr></table>
</td><br />
';
}
}
?>
Now I believe my code is correct as in the way it's set up and by using MySqli. But i'm not sure if its the code or the verification code.
<?php
session_start();
if(!isset($_SESSION['MyID'])||!isset($_SESSION['MyUsername']))
{
header("Location:index.php");
}
?>
Everything seems to line up. My database connections are correct. Just that it loops back to the index page. Unless if there are certain functions that should be outside the if statement. Here is my database code, just to be on the safe side.
<?php
$host = "host";
$db_username = "username";
$db_password = "";
$db_database = "database";
$db = mysqli_connect($host,$db_username,$db_password,$db_database) or die("Errors");
?>
If there is anyway to fix this issue that would be greatly appreciated!