簡體   English   中英

用戶登錄后重定向頁面(php代碼)

[英]redirect page after user login (php code)

在處理檢查用戶名和密碼並且不重定向到主頁后,我在從登錄頁面重定向時遇到問題。 請任何人都可以檢查我的代碼並給我提示或指南(網站)以修復此錯誤。

<?php
    include('session.php');
?>
<?php
    include("config.php");
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        // username and password sent from form 
        $myusername = mysqli_real_escape_string($db,$_POST['uname']);
        $mypassword = mysqli_real_escape_string($db,$_POST['psw']); 
        $sql = "SELECT * FROM admin WHERE UserName = '$myusername' and Password = '$mypassword'";
        $result = mysqli_query($db,$sql);
        $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
        $count = mysqli_num_rows($result);
        // If result matched $myusername and $mypassword, table row must be 1 row
        if($count == 1) {
            $_SESSION['login_user'] = $myusername;
            header("Location: welcome.php");
        }else {
            $error = "<center>
                        <img src='images/logo.jpg'>
                        <p>
                            <font size='4'>Your Login Name or Password is 
invalid</font>
                            <p>
                                <font size='5'><a href='index.php'>Back to MainPage
                                </a>
                                </font>
                    <center>";
            echo "";
        }
    }
?>
    <html>
    <head>
    <title>Login Page</title>
    <style type = "text/css">
       body {
       font-family:Arial, Helvetica, sans-serif;
       font-size:14px;
       }
       label {
       font-weight:bold;
       width:100px;
       font-size:14px;
       }
       .box {
        border:#666666 solid 1px;
       }
    </style>
    </head>
    <body bgcolor = "#FFFFFF">
        <div align = "center">
            <div style = "width:300px; border: solid 1px #333333; " align = "left">
                <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>
                <div style = "margin:30px">
                    <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>   
                </div>  
            </div>
        </div>
    </body>
    </html>

請注意不要在header function 之前向 output 寫入任何字符。 您的代碼以

<?php
    include('session.php');
?>
<?php
    include("config.php");
    ...
?>

避免在您的代碼中使用?>{CR/LF}<?php但使用:

<?php
    include('session.php');
    include('config.php');
    if($_SERVER["REQUEST_METHOD"] == "POST") { 
    ...
?>

檢查session.phpconfig.php是否也寫了一些 Z78E6221F6393D1356ZF688 車主

如果在使用header function 之前將一個字符發送到 output,則永遠不會完成重定向。

在 header 之后添加一個退出function以確保停止代碼執行。

檢查 SQL 注入的先前答案:-)

祝你好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM