Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- login.php
- <?php
- session_start();
- if(isset($_POST)){
- //make connection
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "exam";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- if((isset($_POST['username']) && !empty($_POST['username'])) && (isset($_POST['password']) && !empty($_POST['password']))){
- $username = trim(htmlspecialchars($_POST['username']));
- $password = md5(trim($_POST['password']));
- $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
- $row = $conn->query($query);
- if($fetch = $row->num_rows == 1){
- $guka = $row->fetch_assoc();
- $_SESSION['username'] = $guka['username'];
- $_SESSION['password'] = $guka['password'];
- print_r($_SESSION);
- }else{
- echo "incorrect password";
- }
- }
- $conn->close();
- }
- ?>
- <!DOCTYPE html>
- <html lang="en" dir="ltr">
- <head>
- <meta charset="utf-8">
- <title></title>
- </head>
- <body>
- <form class="" method="post">
- <input type="text" name="username" placeholder="username">
- <input type="password" name="password" placeholder="password">
- <input type="submit" name="" value="submit">
- </form>
- <?php
- if(isset($_SESSION)){
- echo $_SESSION['username'];
- }
- ?>
- </body>
- </html>
- index.php
- <?php
- if(isset($_SESSION)){
- header("Location: login.php");
- }
- ?>
- register.php
- <!DOCTYPE html>
- <html lang="en" dir="ltr">
- <head>
- <meta charset="utf-8">
- <title></title>
- </head>
- <body>
- <form class="" action="check.php" method="post">
- <input type="text" name="name" placeholder="name"><br>
- <input type="text" name="username" placeholder="username"><br>
- <input type="password" name="password" placeholder="password">
- <input type="submit" name="submit" value="submit">
- </form>
- </body>
- </html>
- check.php
- <?php
- //make connection
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "exam";
- // Create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- if (isset($_POST)) {
- if($_POST['username'] === null || empty($_POST['username']) || strlen($_POST['username']) < 4){
- echo "error incorrect username";
- die();
- }
- if($_POST['password'] === null || empty($_POST['password']) || strlen($_POST['password']) < 3){
- echo "error incorrect password";
- die();
- }
- if($_POST['name'] === null || empty($_POST['name'])){
- echo "error incorrect name";
- die();
- }
- $username = trim(htmlspecialchars($_POST['username']));
- $name = trim(htmlspecialchars($_POST['name']));
- $password = md5(trim($_POST['password']));
- $ip = $_SERVER['REMOTE_ADDR'];
- $sql = "INSERT INTO users (username,name,password,ip) VALUES ('$username','$name','$password','$ip')";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- $conn->close();
- header("Location: login.php");
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement