简体   繁体   English

PHP:Localhost重定向您太​​多次

[英]PHP: Localhost redirected you too many times

I want to check in login page if one user already login in the system its go to index page. 我想在登录页面中检查是否有一个用户已经在系统中登录了它的转到索引页面。 But the code i used had an error like this 但是我使用的代码有这样的错误

localhost redirected you too many times. 本地主机将您重定向了太多次。 Try clearing your cookies. 尝试清除您的cookie。 ERR_TOO_MANY_REDIRECTS ERR_TOO_MANY_REDIRECTS

and this is my code to check the user is login or not. 这是我的代码,用于检查用户是否登录。 Thanks 谢谢

<?php
session_start();
include '../pages/koneksi.php';
if(isset($_SESSION['username'])){
    $username = $_SESSION['username'];
    $res = mysqli_query($link, "select * from tb_user where username = '$username';");
    $user = mysqli_fetch_array($res);
    $_SESSION['ID']=$user['ID'];
    header("location: index.php");
    die();
    } else {
       header("location: login.php");
    }
?>

And this is the index file 这是索引文件

<?php
session_start();
include '../pages/koneksi.php';
//check session udah login apa belum
if(isset($_SESSION['username'])){
    $username = $_SESSION['username'];
    $res = mysqli_query($link, "select * from tb_user where username = '$username';");
    $user = mysqli_fetch_array($res);
    $_SESSION['ID']=$user['ID'];
} else {
    header("location: login.php");
}
?>

Do this... On 执行此操作...

index.php index.php

<?php
    session_start();
    include '../pages/koneksi.php';
    if(!isset($_SESSION['username'])){header("location: login.php");}
?>

And on 然后

login.php login.php

<?php
    session_start();
    include '../pages/koneksi.php';
    if(isset($_SESSION['username'])){header("location: index.php");}
?>

Don't add these code on same page... both code are opposite of each other. 不要将这些代码添加到同一页面上...两个代码彼此相反。

If you add them in same page then if or else condition runs every page load.... 如果将它们添加到同一页面中,则条件将运行每个页面加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM