【问题标题】:Session is not following up onto the next webpage会话没有跟进下一个网页
【发布时间】:2016-07-28 19:50:50
【问题描述】:

我在尝试修复第二页上的会话时遇到问题。问题是我无需登录就可以导航到下一页,这一定意味着我没有正确使用会话

这是登录页面:

<?php

    session_start();
    $con = mysqli_connect("localhost","root","","user");

    if ( isset($_POST['username'],$_POST['password']) ) {

        $username = mysqli_real_escape_string($con, $_POST['username']);

        $password = mysqli_real_escape_string($con, $_POST['password']);

        $select_user = "SELECT * FROM user WHERE Username = '$username' AND password ='$password'";

        $run_user = mysqli_query($con, $select_user);

        $check_user = mysqli_num_rows($run_user);

        if ( $check_user > 0 ) {

        $_SESSION['User']= $username;       

        header('location:ConnectionTest.php');

        }

        else {
            echo "wrong username/password";
        }

        mysqli_close($con);
    }

?>

这是登录页面将导航到的页面

<?php

session_start();

    if ( !isset($_SESSION['User']) ) {
        header('location:login.php');
    }

    $con = new mysqli("localhost", "root", "", "schema");

    $result = $con->query("SHOW TABLES");

    while ( $row = $result->fetch_row() ) {

        $table = $row[0];

        echo '<h3>',$table,'</h3>';

        $result1 = $con->query("SELECT * FROM $table");

            if ($result1) {

                echo '<table cellpadding="0" cellspacing="0" class="db-table">';

                    $column = $con->query("SHOW COLUMNS FROM $table");

                    echo '<tr>';

                        while($row3 = $column->fetch_row()) {

                        echo '<th>'.$row3[0].'</th>';

                        } //endwhile $row3

                    echo '</tr>';

                    while($row2 = $result1->fetch_row() ) {

                    echo '<tr>';

                        foreach($row2 as $key=>$value) {

                        echo '<td>',$value,'</td>';

                        } //endforeach

                    echo '</tr>';

                    } //endwhile $row2

                echo '</table><br />';

            } //endif

    } //endwhile $row

$con->close();

?>

【问题讨论】:

  • 位置标头后应始终跟随 die/exit,以防止其余代码执行。仅供参考,您的位置标头一开始是无效的 - 根据定义,位置标头需要绝对 URL。
  • 你正在连接这个 'echo '

    ',$table,'

    ;'错误的。您应该与 [ 连接。 ] 点,如:echo '

    '.$table.'

    ';在同样的情况下进入你的 foreach on echo '
    ',$value,'';回显''.$value.'';此外,您还必须在 IF 语句之后创建一个 ELSE 条件。

标签: php session mysqli


【解决方案1】:

尝试在header() 之后停止脚本。这可以防止already sent headers,因此不会干扰会话:

header(...);
exit;

【讨论】:

  • 我最近刚刚尝试过,它仍然允许我导航到下一页而无需登录。
猜你喜欢
  • 1970-01-01
  • 2022-09-29
  • 2019-04-11
  • 2013-01-28
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 2023-02-05
  • 2021-04-08
相关资源
最近更新 更多