Php 如何检查会话是否已启动并重定向用户?

Php 如何检查会话是否已启动并重定向用户?,php,html,mysql,session,Php,Html,Mysql,Session,我试图创建一个PHP代码片段,检查用户在到达我的索引页面时是否已使用会话注册,如果没有,我会将他们重定向到注册页面 我已经在用户注册后启动了会话,但我似乎无法在我的索引页上检查会话是否已启动 注册PHP: session_start(); //create connection //check for errors in the form if(!$error) { //insert values from form to sql data

我试图创建一个PHP代码片段,检查用户在到达我的索引页面时是否已使用会话注册,如果没有,我会将他们重定向到注册页面

我已经在用户注册后启动了会话,但我似乎无法在我的索引页上检查会话是否已启动

注册PHP:
    session_start();

    //create connection

    //check for errors in the form

    if(!$error) {

        //insert values from form to sql database

        $_SESSION['login_user'] = $username;

        //email user


    }
?>
index.php

<?PHP

    include("signupphp.php");

    if(!$username) {

        echo '<script>location.href = "https://google.com";</script>';

    }

 ?>
session_start()
if( ! $_SESSION['valid']){
    echo '<script>location.href = "https://google.com";</script>';
}


理想情况下,我希望当用户注册时,它会创建会话,无论何时他们进入
index.php
,它都会检查他们是否已注册,如果没有,它会将他们重定向到注册页面。

如果我理解正确,您希望检查会话是否已使用用户名设置,对吗

在这种情况下,您检查的变量是错误的

if( !isset($_SESSION["login_user"]) ){
    header("Location: https://google.com");
    exit();
}

这应该能奏效。我还将您的重定向更改为PHP重定向。

会话将在站点页面之间传播。因此,在注册时:

$username = $_POST['username']//The username could be obtained from a login form
sesion_start();
$_SESSION['login_user'] = $username;
$_SESSION['valid'] = true;
将为index.php保留可用的
$username

session_start()
if( ! $_SESSION['valid']){
    echo '<script>location.href = "https://google.com";</script>';
}
session_start()
如果(!$\会话['valid']){
echo'location.href=”https://google.com";';
}
为什么要将其标记为“mysql”和“html”?