简体   繁体   English

如何在 php 登录成功时重定向或更改 URL 以显示 index.php 页面?

[英]How to redirect or change the URL for display of index.php page on success of login in php?

I have created a Login with session in php but when on password matching (code in loginsubmit.php )我在 php 中使用 session 创建了一个登录,但是在密码匹配时( loginsubmit.php中的代码)

All Login and password checking is working good.所有登录名和密码检查工作正常。 The only problem about redirection to index page with logout option.关于使用注销选项重定向到索引页面的唯一问题。

on success i redirect to index.php but i found that the content of index.php file with logout option is displayed just below the login box and also URL is also not changed.成功后我重定向到index.php但我发现 index.php 文件的内容显示在登录框下方,并且 URL 也没有更改。

I have attached pic and my code of both above file (all these file are in same folder /rtest/).我附上了上述文件的图片和我的代码(所有这些文件都在同一个文件夹/rtest/中)。

1) index.php 1) 索引.php

<body>



<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <button class="navbar-toggle" data-target="#myNavbar" data-toggle="collapse" type="button">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>

        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav navbar-right">

                 <li>
                    <a href="index.php">Home</a>

                </li>
                 <li>
                    <a href="#about">About</a>

                </li>
                 <li>
                    <a href="#contact">Contact Us</a>

                </li>

                <?php

                    session_start();

                    if ( $_SESSION['State'] == 'Authenticated'){
                    ?>

                        <li>
                        <a href='Logout.php'>Logout</a>
                        </li>

                    <?php    

                    }else{

                    ?>

                        <li>
                            <a href='register.php'>Register/Login</a>

                        </li>

                      <?php  
                    }

                ?>


                <li>
                    <a href="#">Admin Login</a>

                </li>

            </ul>
        </div>
    </div>
</nav>

2) loginsubmit.php 2) 登录提交.php

if ($_POST['email'] != "" && $_POST['password'] != "") {

    $email = $_POST['email'];
    $password = $_POST['password'];

    include('connection.php');

    $stmt = $conn->prepare("SELECT Email, Password FROM `tbl_register` WHERE Email = ?");
    $stmt->bind_param( "s", $email); 
    $stmt->execute();
    $stmt->store_result(); 
    $stmt->bind_result($a, $b);   

    if($stmt->fetch() == 0){ 
    header("Location: ../Entrar.php?message=Error");
    exit();
    }
    else {  
        if(password_verify($password, $b)) {

            echo "Correct Password";
            session_start(); 
            $_SESSION['User'] = $a; 
            $_SESSION['State'] = 'Authenticated';  
            header("Location: ./index.php"); 
            exit; 
        }
        else{ 

            echo "Incorrect Password";
            /*header("Location: ../Entrar.php?message=Error");
            exit;*/
        }
    }

    }

    else
    {
        echo "<span style='color:red; font-weight:bold;'>
            Please enter required details.
          </span>";
    }
?>

3) logout.php 3) 注销.php

<?php 


session_destroy(); //Destroy it! So we are logged out now

header("location:index.php");
?>

Picture of problem.问题图片。

login image登录图片

login image with index带索引的登录图像

use this in your register.php page在您的寄存器中使用它。php 页面

if(!isset($_SESSION)){ 
    session_start();
}
if(isset($_SESSION['State'])) {
    header("Location: index.php");
}

in your menu use this在你的菜单中使用这个

if(isset($_SESSION['State'])) {
  //logout
}else{
  //login
}

and in your logout unset the session并在您的注销中取消设置 session

        session_destroy();
        unset($_SESSION['User']);
        unset($_SESSION['State']);

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

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