dougou8552 2013-08-27 18:55
浏览 24

为什么没有显示用户名?

I wrote some simple login script for a school assignment. I need to ask the user to log in redirect them to the main page, and display their username on top of the main page. I've been following the instructions I found online, but the username is not shown in the main page after the user logged in. Can someone take a look at my PHP code and give me some hints on how to resolve this? Thanks!

Here is my main php:

    <?php
    session_start();
    echo "You are logged in as " .$_SESSION['username'];
    echo "<p><a href=\"logout.php\">Click here to logout</a></p>";


    //Turn on error reporting
    ini_set('display_errors', 'On');
    //Connects to the database
    $mysqli = new mysqli("abc", "edf","xyz", "123");
    if($mysqli->connect_errno){
        echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
?>  


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>Test</title>
    <meta charset="UTF-8">  
    <style type="text/css">
        body {font-family:sans-serif;}
        h1 {color: #0000FF;text-align: center;}
        .fieldset-auto-width {display: inline-block;}
    </style>
</head>
<body>
    <div id="header" style="background-color:#FFA500;">
        <h1>Restaurant Review</h1>
    </div>
        //DO SOMETHING HERE
    <div id="content">
    <form method="post" action="addreview.php">     
    </div>
    </form>
</body>
</html> 

Here is my login php

    <?php
    ob_start();

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


    //Turn on error reporting
    ini_set('display_errors', 'On');
    //Connects to the database
    $mysqli = new mysqli("abc", "edf","xyz", "123");
    if($mysqli->connect_errno){
        echo "Connection error " . $mysqli->connect_errno . " " . $mysqli->connect_error;
    }

    $username = mysqli_real_escape_string($mysqli, $username);
    $query = "SELECT password, salt FROM member WHERE username = '$username';";

    $result = mysqli_query($mysqli, $query);

    // User not found. So, redirect to login_form again.
    if (mysqli_num_rows($result) == 0)
    {
        header('Location: login.html');
    }

    $userData = mysqli_fetch_array($result, MYSQL_ASSOC);
    $hash = hash('cs494', $userData['salt'] . hash('cs494', $password));

    //Incorrect password. Redirect to login form again
    if ($hash != $userData['password'])
    {
        header('Location: login.html');
    }else {
    //redirect to main page after successful login
        session_start();
        $_SESSION['username'] = $username;
        header('Location: main.php');
    }


?>
  • 写回答

1条回答 默认 最新

  • douzong0711 2013-08-27 18:59
    关注

    You are echo-ing outside of the HTML document, and it is probably on the page where you cannot see it. If you click View > Source you might see it printed at the top of the document before the <!DOCTYPE> declaration.

    Instead of:

    echo "You are logged in as " .$_SESSION['username'];
    echo "<p><a href=\"logout.php\">Click here to logout</a></p>";
    <!DOCTYPE html>
    <html>...</html>
    

    You should move the echo inside the document like:

    <!DOCTYPE html>
    <html>
    <head>...</head>
    <body>
    <?php
      echo "You are logged in as " .$_SESSION['username'];
      echo "<p><a href=\"logout.php\">Click here to logout</a></p>";
    ?>
    ...
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接