Question
My php code is not directing it to the file that I want it too for index.php. Welcome to Lizard
My php code is not directing it to the file that I want it too for index.php.
session_start(); ob_start(); // Turns on output buffering
$timezone = date_default_timezone_set("America/Chicago");
$con = mysqli_connect("localhost","root", "", "Lizard"); // connection variable
if(mysqli_connect_error()) { echo "Failed to connect" .mysqli_connect_error(); }
// Declaring variables to prevent errors $fname= ""; // First Name $lname= ""; // Last Name $em= ""; // email Name $em2=""; //Email2 $password= ""; // Password $password2= ""; // password 2 $date=""; // Sign up date $error_array= array(); // Holds error messages
if(isset($_POST["register_button"])) {
// Registration form values
// Registration for the first name
$fname=strip_tags($_POST["reg_fname"]); // Remove HTML tags $fname=str_ireplace(' ',' ', $fname); // Removes Spaces $fname=ucfirst(strtolower($fname)); // Uppercase first letter $_SESSION['reg_fname'] = $fname; // Stores first name into session
// Registration for first name
$lname=strip_tags($_POST['reg_lname']); // Remove HTML tags $lname=str_ireplace(' ',' ', $lname); // Removes Spaces $lname=ucfirst(strtolower($lname)); // Uppercase first letter $_SESSION['reg_lname'] = $lname; // register for last name // Registration for email
$em=strip_tags($_POST['reg_em']); // Remove HTML tags $em=str_ireplace(' ',' ', $em); // Removes Spaces $em=ucfirst(strtolower($em)); // Uppercase first letter $_SESSION['reg_em'] = $em; // stores email in session variable // confirmation for email $em2=strip_tags($_POST['reg_em2']); // Remove HTML tags $em2=str_ireplace(' ',' ', $em2); // Removes Spaces $em2=ucfirst(strtolower($em2)); // Uppercase first letter $_SESSION['reg_em2'] = $em2; // stores email in session variable $password=strip_tags($_POST['reg_password']); // Remove HTML tags $password2=strip_tags($_POST['reg_password2']); // Remove HTML tags
$date=date("y-m-d"); // current date }
if($em == $em2) {
// cheack if email is in valid format if(filter_var($em, FILTER_VALIDATE_EMAIL)) { $em = filter_var($em, FILTER_VALIDATE_EMAIL );
// check if email already exists $e_check = mysqli_query($con, "SELECT email FROM users WHERE email='$em'");
// check the number of rows returned
$num_rows = mysqli_num_rows($e_check);
if ($num_rows > 0) { array_push($error_array, "Email already in use ") ; }
} else { array_push($error_array, "Invalid format "); }
} else{ array_push($error_array, "Emails don't match "); }
if(strlen($fname)> 25 || strlen($fname) < 2) { array_push($error_array, "Your first name must be between 2 and 25 characters "); }
if(strlen($lname) > 25 || strlen($lname) < 2) { array_push($error_array, "Your last name must be between 2 and 25 characters "); }
if($password != $password2) { array_push($error_array, "Your passwords do not match "); }
else { if(preg_match('/[^A-Za-z0-9]/', $password)) { array_push($error_array, "Your password can only contain english characters or numbers "); } }
if(strlen($password > 30 || strlen($password) < 5)) { array_push($error_array, "Your passowrd must be between 5 and 30 characters "); }
if(empty($error_array)) { $password = md5($password); // Encrypt password before sending it to database
// Generate username by concatenating first and last name
$username = strtolower($fname . "_". $lname); $check_username_query = mysqli_query($con, "SELECT username FROM users WHERE username='$username'");
$i = 0;
// if username exists add number to username
while(mysqli_num_rows($check_username_query) != 0) { $i++; // Add 1 to i $username = $username ."_". $i; $check_username_query = mysqli_query($con , "SELECT username FROM users WHERE username='$username'"); } // Profile picture assignment
$rand = rand(1,2); // Random number between 1 and 2
if($rand == 1) $profile_pic = "assets/images/profile_pictures/defaults"; else if($rand == 2) $profile_pic = "assets/images/profile_pictures/defaults";
$query = mysqli_query($con, "INSERT INTO users VALUES ('','$fname', '$lname', '$username', '$em', '$password', '$date', '$profile_pic', '0', '0', 'no', ',')");
array_push($error_array, " You're all set! Welcome to Lizard! ");
$_SESSION['reg_fname'] = ""; $_SESSION['reg_lname'] = ""; $_SESSION['reg_em'] = ""; $_SESSION['reg_em2'] = "";
if(isset($_POST['login_button'])) {
$em = filter_var($_POST['log_email'],FILTER_SANITIZE_EMAIL);
// makes sure email is in the correct format
$_SESSION['log_email'] = $em; // Store email into session variable
$password = md5($_POST['log_password']); // Get password
$check_database_query = mysqli_query($con, "SELECT * FROM users WHERE email='$em' AND password = '$password'");
$check_login_query = mysqli_num_rows($check_database_query);
if($check_login_query == 1) { $row = mysqli_fetch_array($check_database_query); // access results from query $username = $row['username'];
$_SESSION['username'] = $username; header("Location: index.php"); exit();
} }
}
?>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started