注册成功后如何登录

时间:2018-04-27 14:23:44

标签: php html mysql forms login

<!DOCTYPE HTML>
<html>
<head>
    <style>
        .error {
            color: red;
        }

        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 10px 25px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
        }

        .div1 {
            background-color: #f2f2f2;
            margin-top: -19px;
            margin-bottom: -25px;
            margin-left: -19px;
        }

        .copy {
            border-radius: 4px;
            padding: 6px 20px;
            border-style: ridge;
        }

        .copy1 {
            border-radius: 4px;
            padding: 6px 28px;
            border-style: ridge;
        }

        .copy2 {
            border-radius: 4px;
            padding: 4px 2px;
        }
    </style>
</head>
<body>
    <?php
    // define variables and set to empty values
    include_once 'connect.php';
    $nameErr = $emailErr = $usernameErr = $passwordErr = $DateOfBirthErr =
        $departmentErr = $ageErr = "";
    $name = $email = $username = $password = $DateOfBirth = $department =
        $age = "";

    if ( isset( $_POST[ 'submit' ] ) ) {
        if ( empty( $_POST[ "name" ] ) ) {
            $nameErr = "Name is required";
        } else {
            $name = test_input( $_POST[ "name" ] );
            // check if name only contains letters and whitespace
            if ( !preg_match( "/^[a-zA-Z ]*$/", $name ) ) {
                $nameErr = "Only letters and white space allowed";
            }
        }

        if ( empty( $_POST[ "email" ] ) ) {
            $emailErr = "Email is required";
        } else {
            $email = test_input( $_POST[ "email" ] );
            // check if e-mail address is well-formed
            if ( !filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
                $emailErr = "Invalid email format";
            }
        }

        if ( empty( $_POST[ "username" ] ) ) {
            $usernameErr = "Username is required";
        } else {
            $username = test_input( $_POST[ "username" ] );
            // check if name only contains letters and whitespace
            if ( !preg_match( "/^[a-zA-Z ]*$/", $username ) ) {
                $usernameErr = "Only letters and white space allowed";
            }
        }

        if ( empty( $_POST[ "password" ] ) ) {
            $passwordErr = "Password is required";
        } else {
            $password = test_input( $_POST[ "password" ] );
            $hashed_password = password_hash( $password, PASSWORD_DEFAULT );
            // check weather password is alphanumeric
            if ( !preg_match( '/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{6,}$/',
                    $password ) ) {
                $passwordErr = "Password must be alphanumeric and atleast 6 characters 
       long!";
            }
        }

        if ( empty( $_POST[ "Date_of_birth" ] ) ) {
            $DateOfBirthErr = "Date Of Birth is required";
        } else {
            $DateOfBirth = test_input( $_POST[ "Date_of_birth" ] );
        }

        if ( empty( $_POST[ "department" ] ) ) {
            $departmentErr = "Department is required";
        } else {
            $department = test_input( $_POST[ "department" ] );
        }

        if ( empty( $_POST[ "age" ] ) ) {
            $ageErr = "Age is required";
        } else {
            $age = test_input( $_POST[ "age" ] );
        }

        if ( $nameErr == "" && $emailErr == "" && $usernameErr == "" &&
            $passwordErr == "" ) {
            $check = "SELECT * FROM users WHERE username = '$_POST[username]'";
            $rs = mysqli_query( $mysqli, $check );
            $da = mysqli_fetch_array( $rs, MYSQLI_NUM );
            if ( $da[ 0 ] > 0 ) {
                echo "Username Already in Exists<br/>";
            } else {
                $sql = "INSERT INTO users(`id`,`username`, `password`, `email` , `name` 
      ,   `Date_of_birth` , `department` ,`age`)
             VALUES ('','" . $username . "', '" . $hashed_password . "', '" . $email . "' , 
       '" . $name . "' , '" . $DateOfBirth . "' , '" . $department . "' , '" . $age . "')";

                if ( mysqli_query( $mysqli, $sql ) ) {
                    echo "Registered successfully";
                } else {
                    echo "Error: " . $sql . "<br>" . mysqli_error( $mysqli );
                }
                mysqli_close( $mysqli );
            }
        }

    }

    function test_input( $data ) {
        $data = trim( $data );
        $data = stripslashes( $data );
        $data = htmlspecialchars( $data );
        return $data;
    }
    ?>

    <div style="padding-left: 250px" class="div1">
        <h2 style="color:#009999">Registration Form :</h2>
        <p><span class="error">All fields are required </span>
        </p>
        <form method="post" action="">
            <span style="color:#0099ff">Name: </span>
            <input type="text" name="name" class="copy" style="margin-left: 52px">
            <span class="error">
                <?php echo $nameErr;?>
            </span>
            <br><br>
            <span style="color:#0099ff"> E-mail: </span>
            <input type="text" name="email" class="copy" style="margin-left: 48px">
            <span class="error">
                <?php echo $emailErr;?>
            </span>
            <br><br>
            <span style="color:#0099ff"> Username: </span>
            <input type="text" name="username" class="copy" style="margin- 
         left:26px">
            <span class="error">
                <?php echo $usernameErr;?>
            </span>
            <br><br>
            <span style="color:#0099ff"> Password: </span>
            <input type="password" name="password" class="copy" style="margin- 
       left:30px">
            <span class="error">
                <?php echo $passwordErr;?>
            </span>
            <br><br>
            <span style="color:#0099ff"> Date Of Birth : </span>
            <input type="date" class="copy1" name="Date_of_birth">
            <span class="error">
                <?php echo $DateOfBirthErr;?>
            </span>
            <br><br>
            <span style="color:#0099ff">  Age : </span>
            <input type="number" name="age" class="copy" style="margin-left:62px">
            <span class="error">
                <?php echo $ageErr;?>
            </span>
            <br><br>
            <span style="color:#0099ff">  Department : </span>
            <select name="department" class="copy2" style="margin-left:14px">
                <option value="EE">Electrical & Electronics</option>
                <option value="EC">Electronics & Communication</option>
                <option value="ME">Mechanical</option>
                <option value="CS">Computer Science</option>
                <option value="CV">Civil</option>
                <option value="IS">Information Science</option>
            </select>
            <span class="error">
                <?php echo $departmentErr;?>
            </span>
            <br><br>
            <input type="submit" class="button" name="submit" value="Register">
            <p style="color:black">Already Registered? <a href="login.php">Login</a>.</p>
        </form>
    </div>
</body>
</html>

login.php

<?php
include_once 'connect.php';

session_start();
$error = "";
if ( isset( $_POST[ 'login' ] ) ) {
    // username and password sent from form 

    $username = mysqli_real_escape_string( $mysqli, $_POST[ 'username' ] );
    $password = mysqli_real_escape_string( $mysqli, $_POST[ 'password' ] );

    $sql = "SELECT id FROM users WHERE username = '$username' and password = 
       '$password'";
    $resu = mysqli_query( $mysqli, $sql );
    $rst = mysqli_fetch_array( $resu, MYSQLI_ASSOC );
    $count = mysqli_num_rows( $resu );

    // If result matched $myusername and $mypassword, table row must be 1 row

    if ( $count == 1 ) {
        $_SESSION[ 'login_user' ] = $username;

        header( "location: index.php" );

    } else {
        $error = "Your Login Name or Password is invalid";
    }

}
if ( isset( $_SESSION[ 'login_user' ] ) ) {
    header( 'location:index.php' );
}
?>

<html>
<head>
    <title>Login Form</title>
    <style>
        body {
            font-family: Arial, Helvetica, sans-serif;
        }

        .frmlg {
            background-color: #99ffff;
            text-align: center;
            margin-top: 10px;
        }

        .xyz {
            margin-top: 30px;
            margin-bottom: 30px;
        }

        .container {
            padding: 16px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 12px 15px;
            border: none;
            cursor: pointer;
            width: 30%;
        }

        input[type=text],
        input[type=password] {
            width: 50%;
            padding: 12px 20px;
            margin: 8px 0;
            display: inline-block;
            border: 1px solid #ccc;
            box-sizing: border-box;
        }
    </style>
</head>

<body>
    <div class="container frmlg">
        <form method="post" action="" enctype="multipart/form-data">
            <h4 style="text-align:center">Login Page</h4>
            <div class="container xyz">
                <label for="uname"><b>Username :</b></label>
                <input type="text" placeholder="Enter Username" name="username" required>
                <br><br>
                <label for="psw"><b>Password :</b></label>
                <input type="password" placeholder="Enter Password" name="password" required>
                <br><br>
                <button type="submit" name="login" value="Submit">Login</button><br/><br/>
            </div>
        </form>
        <div style="font-size:11px; color:#cc0000; margin-top:10px">
            <?php 
         echo $error; ?>
        </div>
    </div>
</body>

</html>

logout.php

<?php
session_start();

if ( session_destroy() ) {
    header( "Location: login.php" );
}
?>

session.php文件

<?php
include_once 'connect.php';
session_start();

$user_check = $_SESSION[ 'login_user' ];

$ses_sql = mysqli_query( $mysqli, "select username from users where 
       username = 
        '$user_check' " );

$row = mysqli_fetch_array( $ses_sql, MYSQLI_ASSOC );

$login_session = $row[ 'username' ];

if ( !isset( $_SESSION[ 'login_user' ] ) ) {
    header( "location:login.php" );
}
?>

的welcome.php

<?php include_once 'session.php'; ?>
<html>
<head>
<title>Welcome </title>
</head>
<body>
    <h1>Welcome <?php echo $login_session; ?></h1> 
    <h2><a href = "logout.php">Sign Out</a></h2>
</body>
</html>

connect.php

<?php
$databaseHost = 'localhost';
$databaseName = 'amith';
$databaseUsername = 'root';
$databasePassword = '';

$mysqli = mysqli_connect($databaseHost, $databaseUsername, 
$databasePassword, $databaseName);
?>

我正在创建一个简单的php注册表单和一个登录表单。我已经完成了注册表格并且运行良好,但现在在他们尝试登录到他们的页面后提交注册表格之后它应该登录但是我无法得到这个期望的结果可以任何人指导我应该做什么改变对上面的代码做,以便获得我的结果。

注意:我尚未添加我的注册文件,但我已将该文件保存为index.php

2 个答案:

答案 0 :(得分:0)

在成功注册代码

之后添加此代码
$_SESSION['login_user'] = $username;

$username是通过表单

发布的用户名

然后是像

这样的标题
header('Location:welcome.php');

答案 1 :(得分:0)

将查询部分更新为此

if (mysqli_query($mysqli, $sql)) {
    echo "Registered successfully";
    $_SESSION['login_user'] = $username; //sets user session to the username
    header('Location:welcome.php'); //redirects the user to the user home page
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($mysqli);
 }