在其他设备上打开网站时无法访问会话变量[php code]

时间:2017-03-19 10:20:19

标签: php

我有本地主机网站。将两个设备连接到热点并使用服务器的IP访问网站。所有页面都正常打开,但会话变量未被识别为"未定义索引"错误抛出。

当我在本地主机上访问相同内容时,我不会面对这个问题。

我在所有文件中都使用了session_start()。

将代码剪切为Login.php

<?php
session_start();

?>
<html>
<head><title>LOGIN</title></head>
<body>
<form action= "Dashboard.php" method = "POST">

        <br/>Name: <input type = "text" name = "name">
        <br/>Password: <input type = "password" name = "password">
        <br/><input type="submit" name = "submit" value = "submit"> or <a href="Register.php">Register</a>
</form>
</body>
</html>




<?php   

if(isset($_POST['submit'])){

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

    $connect = mysqli_connect("localhost","root", "","nets") or die("Couldn't connect to database");

    $query = mysqli_query($connect,"SELECT * FROM users WHERE Name = '$name' and Password='$password'");
    $count = mysqli_num_rows($query);

    if($count == 1){
        $_SESSION['username'] = $name;
        echo "Login Successful";
    }else{
        echo "Invalid Login Credentials";
    }
}
?>

登录Dashboard.php后

    <?php
session_start();

?>
<html>
<head><title>Dashboard</title></head>
<body>
<form action= "" method = "POST">
        <?php echo "Welcome ".$_SESSION['username'].".<br>";        
        ?>
        <br/>Sex: <select name="sex"> <option value ="Male">Male</option> <option value = "Female">Female</option></select>
        <br/>Age: <select name="years">
            <?php 
                for($i=1; $i<=50; $i++)
                {
                echo "<option value=".$i.">".$i."</option>";
                }
            ?> 
                 <option name="years"> </option>   
            </select> 
        <br/>Citizen: <select name="citizen"><option value="Singaporean">Singaporean</option><option value="International">International</option></select>

        <a href="Download.php">Click to Download!</a>

        <br/><input type="submit" name = "submit2" value = "Save Changes"> or<a href="Login.php">Logout</a> 


</form>
</body>
</html>

<?php   

if(isset($_POST['submit2'])){

    $name = $_SESSION['username'];

            $connect = mysqli_connect("localhost","root", "","nets") or die("Couldn't connect to database");
            $query1 = mysqli_query($connect,"SELECT Sex,Age,Citizen FROM users where Name = '$name'");
             while ($row = mysqli_fetch_row($query1))
            /* {
                echo "Details Entered in Database:".".<br>";
                echo "<br>";
                echo "Name:".$_SESSION['username'].".<br>"; 
                echo "Sex:".$row[0].".<br>";
                echo "Age:".$row[1].".<br>";
                echo "Citizen:".$row[2].".<br>";
             }*/
             $sex = $_POST['sex'];
    $years = $_POST['years'];
    $citizen = $_POST['citizen'];
             $query2 = mysqli_query($connect, "UPDATE users SET Sex = '$sex', Age='$years', Citizen='$citizen' WHERE Name = '$name'");
             echo "Saved changes";
}

?>

请建议

1 个答案:

答案 0 :(得分:1)

您的表单包含操作&#34; Dashboard.php&#34;,以便脚本将处理您的登录表单的提交。

换句话说,if语句if(isset($_POST['submit'])){的代码块内部的逻辑可能没有被执行,所以你的会话变量没有被设置。

您可以将操作设置为&#34; Login.php&#34;或者将其留空以让Login.php处理登录,然后你可以根据需要重定向到Dashboard.php并设置会话变量。

请注意,如果要使用PHP重定向,则必须在HTML之前完成。否则它就不会起作用。