Avatar of Siobhan Williams
Siobhan Williams
 asked on

trouble with the login script in php

when i enter the username and password and click on enter I keep getting Login failed. I keep wondering if to add a num row statement to create the session.


<?php
   //initialize a feedback variable
   $feedback = ""; 
   
    //retrieve the form details from the POST global variable
	$f = $_POST['firstname'];
	$l = $_POST['lastname'];
	$u = $_POST['username'];
	$e = $_POST['email'];
	$p = $_POST['password'];
	$c = $_POST['captcha_code'];
	
	   //echo "Data retieved from form<br/>";
	
	//validate the data 
	validate($f, $l, $u, $e, $p, $c);
	 
	    //echo "Data validated<br/>";
		
		// call made after validation to check presence
	verifyCaptchaValue($c);
		//echo "captcha validated<br/>";
		
	if($feedback != ""){ // validation has failed
		Header("Location:../presentation/registerUser.php?feedbackMsg=$feedback");
		//echo "Data validation failed<br/>";
		
	}else{
		/*$feedback = "validation passed";
		Header("Location:../presentation/registerUser.php?feedbackMsg=$feedback"); //-- for testing*/
		
		//sanitize data
		$f = sanitize($f);
		$l = sanitize($l);
		$u = sanitize($u);
		$e = sanitize($e);
		$p = sanitize($p);
		
		//echo "Data snaitize<br/>";
		
		//hash password for storage
		$p = md5($p); 
		//echo "Password hashed<br/>";
		
		//CONNECT TO DB SERVER AND SELECT DB/
		require("../data/dbconnection.php");
		
		//echo "connected to db<br/>";
		
		// PREPARE SQL STATEMENT
		if ($stmt = mysqli_prepare($mysqli,
		     "INSERT INTO tbluser(firstname, lastname, username, email, password, confirm_code, active)
			 VALUES (?, ?, ?, ?,?, ?, ?")){
			
				 
			
            //echo "statement prepared<br/>";
			
		 //BIND PARAMETERS TO SQL STAEMENT OBJECT
		 mysqli_stmt_bind_param($stmt, "sssssss", $f, $l, $u, $e, $p, $confirm_code, $active);
				 
				 //echo "Parameters bound<br/>";
				 
		//EXECUTE STATEMENT OBJECT AND CHECK IF SUCCESSFUL
		      if(mysqli_stmt_execute($stmt)){
				  
					 $feedback = "Add User Successfully";
					 emailMbr($e, $f);
					//echo "statement executed - $feedback<br/>";
				 }else{
					 
					 $feedback = "Add User Unsuccessfully";
					 //echo "statement failed executed - $feedback<br/>";
				 }
				
               	
			 }//end prepare stmt block
			 
			 Header("Location:../presentation/registerUser.php?feedbackMsg=$feedback");
			 
			 
	}//end validation block
	
function emailMbr($e, $name){	
	
//random code
$confirm_code= getCode(7);



// ---------------- SEND MAIL FORM ----------------

// send e-mail to ...
$to=$e;

// Your subject
$subject="Activation Link For Your Account";

// Your Header Information
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$header .="From:WAD<wsiobhan492@gmail.com>";

// Your message
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body style='background-color:pink'>
	<h3 bgcolor='#0099ff'>Your Activation Link</h3>
	<p>Hey $name,
	<br/>Please click on the link below to activate your account status</p>
	<a href='http://localhost/YOUR SITE ACTIVATION SCRIPT AND CODE HERE'>Click Here</a> To activate your account.
</body>
</html>";

// send email using PHP mail function 
ini_set("smtp_port","465"); 
$sentmail = mail($to,$subject,$message,$header);

// if your email succesfully sent
if($sentmail){
 echo "<p>Your Confirmation link Has Been Sent To Your Email Address.";
}
else {
 echo "Cannot send Confirmation link to your e-mail address";
}
}

//code genarator

function getCode($len){
	$result = "";
     $chars = "abcdefghijklmnopqrstuvwxyz$?!-0123456789";
     $charArray = str_split($chars);
       for($i = 0; $i < $len; $i++){
	     $randItem = array_rand($charArray);
	     $result .= "".$charArray[$randItem ];
}
     return $result;
}
	
	
	
	//functions at the bottom of the page
	
	//function to SANITIZE (Clean) data
	function sanitize($data){
		$data = trim($data);
		$data = stripslashes($data);
		$data = filter_var($data, FILTER_SANITIZE_STRING);
		$data = filter_var($data, FILTER_SANITIZE_SPECIAL_CHARS);
		$data = filter_var($data, FILTER_SANITIZE_MAGIC_QUOTES);
		
		//format data for storage (maintain uniformity)
		$data = strtolower($data); //lowercase
		$data = ucfirst($data); //uppercase first character of string
		
		//finally .... return the cleaned and formatted data
		return $data;
		
	} // end function sanitize
	
	
	//function to VALIDATE data
	function validate($fVal, $lVal, $uVal, $eVal, $pVal, $cVal){
		global $feedback;
		
		if($fVal="" || $fVal==null){
			$feedback  .= "Firstname required.<br/>";
		}
		
		if($lVal="" || $lVal==null){
			$feedback  .= "Lastname required.<br/>";
		}
		
		if($uVal="" || $uVal==null){
			$feedback  .= "Username required.<br/>";
		}
		
		if($eVal="" || $eVal==null){
			$feedback  .= "Email required.<br/>";
		}
		
		/*if (!preg_match("/[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+.[a-zA-Z]{2,4}/",  $e))
		{
        $feedback  .= "Email invalid.<br/>"; //Email address is invalid.
        }*/
				
		if($pVal="" || $pVal==null){
			$feedback  .= "Password required.<br/>";
		}
		
	} // end validate method
	
	function verifyCaptchaValue($cVal){
		global $feedback;
		
		include_once '/securimage/securimage.php';
	
	$securimage = new Securimage();
	
	if ($securimage->check($_POST['captcha_code']) == false) {
		// the code was incorrect
		
       $feedback .= "Incorrect captcha";
	  
	}

	}
?>

Open in new window

PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Julian Hansen

@Siobhan, looks like you are new to EE - welcome.
A convention on the site is to use CODE tags to enclose your code snippets - it makes your question easier to read and to refer to.
To add CODE tags, highlight the code and click the CODE button in the toolbar.
I have edited your question and added them for you.
Julian Hansen

Looks like you posted a registration script - can't see where the login is handled.
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Siobhan Williams

ASKER
Sorry about that I have not realize I posted the wrong script I'll re-post the login script in a moment.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Siobhan Williams

ASKER
Also kindly look at the register script because I am unable to send an email because  this
(/*if (!preg_match("/[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+.[a-zA-Z]{2,4}/",  $e))
		{
        $feedback  .= "Email invalid.<br/>"; //Email address is invalid.

Open in new window

which keep telling email invalid when testing the code, not sure where I see the error in the script because this is my first time doing this. thank you
SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

stale question