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

时间:2018-06-03 06:56:18

标签: php html

sign_up page

<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();
?>

2 个答案:

答案 0 :(得分:0)

更改

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'parent_id', 'name'
    ];

    /**
     *
     */
    public function children()
    {
        return $this->hasMany(Category::class,  'parent_id', 'id');
    }

    /**
     *
     */
    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id', 'id');
    }
}

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

并在表单顶部移动php循环和数据库连接

答案 1 :(得分:0)

如果以上是您网页的实际代码,那么就会出现问题。要使用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>

希望它有所帮助...