Redirect to login page in php but can not access home.php when login successfully. Please help
index.php
<?php
session_start();
require_once 'class.user.php';
$user_login = new USER();
if($user_login->is_logged_in()!="")
{
$user_login->redirect('home.php');
}
if(isset($_POST['btn-login']))
{
$email = trim($_POST['txtemail']);
$upass = trim($_POST['txtupass']);
if($user_login->login($email,$upass))
{
$user_login->redirect('home.php');
}
}
?>
<!DOCTYPE html>
<html>
</html>
home.php
<?php
include("checklogin.php");
check_login();
?>
<!DOCTYPE html>
<html>
</html>
checklogin.php
<?php
function check_login()
{
if(strlen($_SESSION['login'])==0)
{
$host=$_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra="index.php";
$_SESSION["login"]="";
header("Location: http://$host$uri/$extra");
}
}
?>
before logging on to home.php will redirect to index.php. When successful login, the error rate Too Many Redirects
You need to add session_start();
for your home page code. You are getting too many redirects error since session is not started on home.php
<?php
session_start();
include("checklogin.php");
check_login();
?>
<!DOCTYPE html>
<html>
</html>
Hope it helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.