Advertisement
hasanmisbah

ajax-register.php

Nov 13th, 2017
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. <?php
  2.     // allow the Config
  3.     define('__CONFIG__', true);
  4.     // require the config
  5.     require_once '../inc/config.php';
  6.  
  7. if($_SERVER['REQUEST_METHOD']=='POST' or 1==1){
  8.  
  9.     // Always return JSON Format
  10.     //header('Content-Type : application/json');
  11.     $return = [];
  12.  
  13.     $email = Filter::String($_POST['email']);
  14.  
  15.     //make sure the user does not exist
  16.     $findUser = $con->prepare('SELECT user_id FROM users WHERE email= LOWER(:email) LIMIT 1');
  17.     $findUser->bindParam(':email' ,$email, PDO::PARAM_STR);
  18.     $findUser->execute();
  19.  
  20.     if(findUser -> rowCount == 1){
  21.         // user exist
  22.         $return['error'] = 'the email address already registered in our database.';
  23.  
  24.         $return['is_logged_in'] = false;
  25.     }else{
  26.         // user can be register
  27.        
  28.         $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
  29.  
  30.         $addUser = $con->prepare('INSERT INTO users(LOWER(email), password) VALUES(LOWER(:email) , :password)');
  31.         $addUser->bindParam(':email' , $email, PDO::PARAM_STR);
  32.         $addUser->bindParam(':password' , $password, PDO::PARAM_STR);
  33.         $addUser->execute();
  34.  
  35.         $user_id = $con -> lastInserId();
  36.  
  37.         $_SESSION['user_id'] = int() $user_id;
  38.  
  39.         $return['redirect'] = '/dashboard.php?message=welcome';
  40.         $return['is_logged_in'] = true;
  41.     }
  42.  
  43.     echo json_encode($return, JSON_PRETTY_PRINT); exit;
  44.     }else{
  45.         exit('invalid URL');
  46.     }
  47.  
  48.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement