简体   繁体   English

用户登录后重定向页面(php代码)

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

I have problem in redirect from login page after process check username and password and not redirect to main page.在处理检查用户名和密码并且不重定向到主页后,我在从登录页面重定向时遇到问题。 please anyone can check my code and give me hint or guide(website) to repair this error.请任何人都可以检查我的代码并给我提示或指南(网站)以修复此错误。

<?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>

Be carrefull to not write any caracter to the output before header function.请注意不要在header function 之前向 output 写入任何字符。 Your code start with您的代码以

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

Avoid using ?>{CR/LF}<?php in you code but use:避免在您的代码中使用?>{CR/LF}<?php但使用:

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

Check if session.php and config.php writes some output caracters too...检查session.phpconfig.php是否也写了一些 Z78E6221F6393D1356ZF688 车主

If one caracter is sent to output before using header function, the redirection will never be done.如果在使用header function 之前将一个字符发送到 output,则永远不会完成重定向。

Add an exit or die function just after the header one to be sure to stop code execution.在 header 之后添加一个退出function以确保停止代码执行。

Check previous answers for SQL injection too:-)检查 SQL 注入的先前答案:-)

Good Luck.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM