Demo entry 6783431

asd

   

Submitted by anonymous on Feb 11, 2019 at 13:34
Language: PHP. Code size: 2.6 kB.

<?php
   session_start();
   $username= "";
   $email="";
   $errors = array ();
   // konektimi me databazen
   $db = mysqli_connect('localhost', 'root', '', 'registration');

   //nqs klikohet butoni register

   if (isset($_POST['register'])) {
       $username = mysql_real_escape_string($_POST['username']);
       $email = mysql_real_escape_string($_POST['email']);
       $password_1 = mysql_real_escape_string($_POST['password_1']);
       $password_2 = mysql_real_escape_string($_POST['password_2']);
       //sigurohu qe fushat jane te mbushura 
       if (empty($username)) {
           array_push($errors, " Username is required") ;
        }
       if (empty($email)) {
        array_push($errors, " email is required") ;
    }
    if (empty($password_1)) {
        array_push($errors, " Password  is required");
    }
    if ($password_1 != $password_2) {
        array_push($errors, "The two passwords do not match");
    }
    //nqs nuk ka errors save the user to the database
    if(count($errors) == 0) {
        $password = md5($password_1); //enkriptimi
        $sql = "INSERT INTO users (username, email, password)
         VALUES('$username' , '$email', '$password')";
         mysqli_query($db, $sql);
         $_SESSION['username']= $username;
         $_SESSION['success'] = "You are now logged in";
         $_SESSION['user_id'] = $db->insert_id;
         header('location: index.php'); //redirekt ne homepage
    }

   }

   //log user from login page 
   if (isset($_POST['login'])) {
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    
    
    if (empty($username)) {
        array_push($errors, " Username is required") ;
     }
   
    if (empty ($password)) {
     array_push($errors, "Password is required");
    }
    
    if (count($errors) == 0 ) {
        $password = md5($password);
        $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $results = mysqli_query($db, $query);

        if (mysqli_num_rows($results) == 1) {
          $_SESSION['username'] = $username;
          $_SESSION['success'] = "You are now logged in";
          header('location: index.php');
        }else {
            array_push($errors, "Wrong username/password combination");
        }
    }
  }



   //logout
   if (isset($_GET['logout'])) {
       session_destroy();
       unset($_SESSION['username']);
       header('location: login.php');
   }



?>

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).