繁体   English   中英

无法使用php重定向到登录页面

[英]unable to redirect to login page using php

注册页面

<html>
    <head>
        <meta name="keywords" content="example, Javascript Form Validation, Sample registration form" />
    </head>
    <body>
        <form name="registration" method="post" action= '<?php $_SERVER["PHP_SELF"] ?>'>
            <label for="user">USERNAME</label>
            <input name="user" type="text" id="username" placeholder="should not contain spaces" required><br><br>
            <label for="email">EMAIL</label>
            <input name="email" type="email" id="email" placeholder="eg: abc@xyz.com" required><br><br>
            <label for="pass">PASSWORD</label>
            <input name="pass" type="password" id="password" placeholder="atleat 8 characters" required><br><br>
            <label for="conf_pass">CONFIRM PASSWORD</label>
            <input name="conf_pass" type="password" id="conf_pass" placeholder="atleat 8 characters" required><br><br>
            <label for="mobile">MOBILE NO</label>
            <input name="mobile" type="number" id="mobile" placeholder="should contain 10 digits" required><br><br>
            <label for="dob">DATE OF BIRTH</label>
            <input name="dob" type="date" id="dob" required><br><br>

            <input type="submit" name="signup" id="submit" value="Submit">
        </form>
    </body>
</html>
===============================================================================
<?php
    $conn=new mysqli("localhost","khushank","sethi","emp");
    if(!$conn){
        echo "unable to connect".$conn->connect_error();
    }
    if($_SERVER['REQUEST_METHOD']=='POST') {
        if(isset($_POST['signup'])){
            $user=$_POST['user'];
            $email=$_POST['email'];
            $psw=$_POST['pass'];
            $conf_psw=$_POST['conf_pass'];
            $mob=(int)$_POST['mobile'];
            $dob=$_POST['dob'];
            if($psw!=$conf_psw){
                echo"<script type='text/javascript'>".'alert(confirm password and password should be same");
                </script>';
            }
            else{
                $sel="SELECT userid FROM details WHERE userid='$user'";
                $sql="INSERT INTO details(userid,email,pass,mobile,date_of_birth) VALUES(?,?,?,?,?)";
                $res=$conn->query($sel);
                if($res->num_rows!=0){
                    echo"<script type='text/javascript'>".'alert("username already exists");
                    </script>';
                } 
                else{           
                    $stmt=$conn->prepare($sql);
                    $stmt->bind_param('sssis', $user, $email, $pass, $mob, $dob);
                    if($stmt->execute()){
                        header('location:login.php');
                    } 
                $stmt->close();
                }
            }
        }
    }
    $conn->close();
?>

更改

action= '<?php $_SERVER["PHP_SELF"] ?>

action= '<?php echo $_SERVER["PHP_SELF"] ?>

并将php循环和db连接移到表单顶部

如果以上是您页面的实际代码,则存在问题。 要使用header您需要在将任何HTML内容发送到浏览器之前执行此操作(尽管您可以使用output buffering来缓解此要求,但不是最佳实践)-因此很可能会记录错误,但显然不会显示错误,否则您会在问题中提到他们了吗?

同样,下面的线也会导致错误。

echo"<script type='text/javascript'>".'alert(confirm password and password should be same");
</script>';

进行一些小改动-将php代码放在HTML之前应该可以解决与header使用有关的错误,简化javascript变量和后续显示。 删除PHP_SELF作为表单操作-它很容易被滥用,并且当您发布到同一页面时,根本不需要。

<?php

    try{

        $message=false;


        if( $_SERVER['REQUEST_METHOD']=='POST' ) {

            $conn=new mysqli( "localhost", "khushank", "sethi", "emp" );
            if( !$conn ) throw new Exception( sprintf( "Unable to connect: %s", $conn->connect_error() ), 1 );          

            /*
                ensure that other required fields are in the submitted data
            */
            if( isset( 
                $_POST['signup'],
                $_POST['user'],
                $_POST['email'],
                $_POST['pass'],
                $_POST['conf_pass'],
                $_POST['mobile'],
                $_POST['dob']
            )){

                $user=$_POST['user'];
                $email=$_POST['email'];
                $psw=$_POST['pass'];
                $conf_psw=$_POST['conf_pass'];
                $mob=(int)$_POST['mobile'];
                $dob=$_POST['dob'];

                /* passwords do not match - abort processing here */
                if( $psw !== $conf_psw ){

                    $message='confirm password and password should be same';

                } else{

                    /* try to locate user in db */
                    $sql="select `userid` from `details` where `userid`=?";
                    $stmt=$conn->prepare( $sql );

                    if( !$stmt ) throw new Exception( 'Failed to prepare sql query', 3 );
                    $stmt->bind_param('s', $user );
                    $res=$stmt->execute();


                    if( $stmt->num_rows == 0 ){
                        /* user does not already exist */
                        $sql="insert into `details` ( `userid`, `email`, `pass`, `mobile`, `date_of_birth` ) values( ?,?,?,?,? )";

                        /* clean up from previous query */
                        $stmt->free_result();
                        $stmt->close();

                        /* prepare new query */
                        $stmt=$conn->prepare( $sql );
                        if( !$stmt ) throw new Exception( 'Failed to prepare sql query', 4 );

                        /* bind and execute */
                        $stmt->bind_param( 'sssis', $user, $email, $pass, $mob, $dob );
                        $result = $stmt->execute();

                        /* this should be exactly 1 */
                        $count = $conn->affected_rows;
                        $stmt->close();
                        $conn->close();

                        if( $count===1 )header( 'Location: login.php' );

                    } else {
                        $message='username already exists';
                    }
                }

                $conn->close();

            } else {
                throw new Exception( 'Missing or empty values in POST data', 2 );
            }
        }
    }catch( Exception $e ){
        exit( sprintf( 'Exception: %s, Code: %d', $e->getMessage(), $e->getCode() ) );
    }
?>

<html>
    <head>
        <meta name='keywords' content='example, Javascript Form Validation, Sample registration form' />
        <title>User registration</title>
    </head>
    <body>
        <form name='registration' method='post'>
            <label for='user'>USERNAME</label>
            <input name='user' type='text' id='username' placeholder='should not contain spaces' required>
            <br><br>

            <label for='email'>EMAIL</label>
            <input name='email' type='email' id='email' placeholder='eg: abc@xyz.com' required>
            <br><br>

            <label for='pass'>PASSWORD</label>
            <input name='pass' type='password' id='password' placeholder='atleat 8 characters' required>
            <br><br>

            <label for='conf_pass'>CONFIRM PASSWORD</label>
            <input name='conf_pass' type='password' id='conf_pass' placeholder='atleat 8 characters' required>
            <br><br>

            <label for='mobile'>MOBILE NO</label>
            <input name='mobile' type='number' id='mobile' placeholder='should contain 10 digits' required>
            <br><br>

            <label for='dob'>DATE OF BIRTH</label>
            <input name='dob' type='date' id='dob' required>
            <br><br>

            <input type='submit' name='signup' id='submit' value='Submit'>
        </form>
        <?php

            /* If there was an error, use a javascript alert to inform user */
            if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $message ) ){
                printf( '<script>alert("%s");</script>', $message );
            }

        ?>
    </body>
</html>

希望能帮助到你...

暂无
暂无

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

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