Reputation: 13
Hi guys I'm trying to start new session in PHP CMS Application but when i tried to start session with username and password to access main.php page it is redirecting to login page and showing no error
This is my login.php script:
<?php
session_start();
require('../config/connect.php');
if (isset($_POST['userid']) and isset($_POST['password'])){
$username = $_POST['userid'];
$password = md5($_POST['password']);
$query = "SELECT * FROM `users` WHERE username='$username' AND password='$password'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
$row= mysql_fetch_array($result);
$userid = $row['userid'];
$usertype= $row['usertype'];
if ($count == 1){
$_SESSION['userid'] = $userid;
$_SESSION['username'] = $username;
$_SESSION['usertype'] = $usertype;
if($_SESSION['usertype'] == 'ADMIN'){
// echo "ADMIN AREA";
header("Location:../admin/index.php");
}elseif ($_SESSION['usertype'] == 'STUDENT'){
//echo "STUDENT AREA";
header("Location:../students/index.php");
}else{
echo "CLINT AREA";
}
}else{
echo "<script>
alert(' Invalid Username And Password');
window.location.href='../index.php';
</script>";
}
}
?>
and this is my session start code
<?php
session_start();
if(isset($_SESSION['logged_in'])){
echo 'Start Session now';
}
else
{
header("Location:../index.php");
}
?>
Upvotes: 0
Views: 141
Reputation: 1731
Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening
After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)
Make sure cookies are enabled in the browser you are using to test it on.
Upvotes: 0
Reputation: 3849
You did not use logged_in index in your code.
So, you should change your code like @Mr7-itsurdeveloper provided answer.
OR
<?php
session_start();
if(!empty($_SESSION['userid']) && !empty($_SESSION(username)))
{
header("Location:../index.php");
}
else
{
header("location:login.php");
}
?>
Upvotes: 0
Reputation: 1631
you already put userid
$_SESSION['userid'] = $userid; in session .you didn't put logged_in
in session .
so use userid
instead logged_in
<?php
session_start();
if(empty($_SESSION['userid']))
{
header("location:login.php");
}
else
{
header("Location:../index.php");
}
?>
Upvotes: 0