Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //session_start allows us to access the session array
- session_start();
- //inluding connection.php connects us to the database
- require_once('connection.php');
- //store success messages, error messages in data array
- $data = NULL;
- //redirect user to a page and pass data via session
- function redirect($session_data, $url)
- {
- $_SESSION = $session_data;
- header('location:'.$url);
- }
- //if re_password fied isset process registration else login
- if(isset($_POST['re_password']))
- {
- //validate email and password, set validation errors
- if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === FALSE)
- $data['errors'][] = "Invalid email";
- if($_POST['password'] != $_POST['re_password'] OR $_POST['password'] == NULL)
- $data['errors'][] = "Passwords do not match";
- //if no errors and email is not in use then add user
- if($data['errors'] == NULL)
- {
- $check_user = $connection->query("SELECT * FROM users WHERE users.email = '".$_POST['email']."' ")->fetch_assoc();
- if($check_user == NULL)
- {
- $new_user = $connection->query("INSERT INTO users (username, first_name, last_name, email, password) ('','".$_POST['username']."','".$_POST['first_name']."', '".$_POST['last_name']."', '".$_POST['email']."', '".md5($_POST['password'])."')");
- if($new_user === TRUE)
- {
- $data['registered'] = $_POST['email'];
- redirect($data, 'login.php');
- }
- }
- else
- {
- $data['errors'][] = "Email already in use";
- redirect($data, 'register.php');
- }
- }
- else
- {
- $date['success'] = "You are now Registered... Please Login";
- redirect($data, 'register.php');
- }
- }
- else
- {
- //check if user exist in database with given email and password
- $check_user = $connection->query("SELECT * FROM users WHERE users.email = '".$_POST['email']."' AND users.password = '".md5($_POST['password'])."' ")->fetch_assoc();
- //if user exist set session variables and redirect user to profile page
- if($check_user != NULL)
- {
- $_SESSION['id'] = $check_user['id'];
- $_SESSION['email'] = $check_user['email'];
- $_SESSION['login'] = TRUE;
- if($_SESSION['login'] === TRUE)
- header('location: user_profile.php?id='.$check_user['id']);
- }
- else
- {
- $data['errors'][] = "Invalid email or password";
- redirect($data, 'login.php');
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement