This question already has an answer here:
my login page doesnt redirect me to the next webpage even if i inputted correct data on username and password.
when i check the config.inc and made it to config.php
it doesnt displays anything on the webpage.
any suggestions
CONFIG.INC
$con=mysqli_connect("localhost","root@localhost","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
LOGINPROC PAGE
<?php
// Inialize session
session_start();
// Include database connection settings
include('config.inc');
// Retrieve username and password from database according to user's input
$login = mysql_query("SELECT * FROM users WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "')");
// Check username and password match
if (mysql_num_rows($login) == 1) {
// Set username session variable
$_SESSION['username'] = $_POST['username'];
// Jump to secured page
header('Location: securedpage.php');
}
else {
// Jump to login page
header('Location: index.php');
}
?>
securedpage.php
<?php
// Inialize session
session_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['username'])) {
header('Location: index.php');
}
?>
<html>
<head>
<title>Secured Page</title>
</head>
<body>
<p>This is secured page with session: <b><?php echo $_SESSION['username']; ?></b>
<br>You can put your restricted information here.</p>
<p><a href="logout.php">Logout</a></p>
</body>
</html>
</div>
Make sure nothing is output before the header function, including any white space in your PHP file and any included PHP files
Also check the files for any BOM unicode, this is usually an option in the editor applications , I would suggest you to comment the header function and use a echo to check if the if or the else are being done
does your form in the same login page or a separate page? if it's in the same login page, try using an if ($_POST['submit'])
then run your code. if it's in a separate page make sure in your form action you have the path for the login page.