It sound a Stupid question but I don know what is the problem each time i try to declare a variable inside a function it gives me an error !
function emptyInputSignup($name, $email, $password, $confirm_password)
{
$result;
if (empty($name) || empty($email) || empty($password) || ($confirm_password)) {
$result = true;
} else {
$result = false;
}
return $result;
}
I've tried to declare it and i got an error.
You don't declare variables in PHP, you define them.
There's no need for $result; and it actually means "read the value of the variable $result and then do nothing with it" - which will give you an undefined variable error if $result isn't defined (which, in your case, it is).
What you have to do instead is initialize $result to some value, or leave it out completely:
$result = false;
For example.
Your code can be greatly simplified by just using assigment:
$result = empty($name) || empty($email) || empty($password) || ($confirm_password);
No need for the if. And you can actually ditch the $result variable entirely:
function emptyInputSignup($name, $email, $password, $confirm_password) {
return empty($name) || empty($email) || empty($password) || ($confirm_password);
}
You can initialize a variable like this:
$result = NULL;
You can try using any of the following:
{
$result = false;
if (empty($name) || empty($email) || empty($password) || ($confirm_password)) {
$result = true;
}
return $result;
}
// or
{
if (empty($name) || empty($email) || empty($password) || ($confirm_password)) {
return true;
}
return false;
}
// or
{
return (empty($name) || empty($email) || empty($password) || ($confirm_password));
}
All these constructs produce the same result.
Related
I am following a tutorial on how to create a sign up page in php, but I keep getting an empty input error when I type results into the sign up section. I think that is because I am getting an error that my $result variable is undefined. I have re-watched the tutorial many times to find any typos but have ended up just exhausting myself.
This is my .signup.inc.php file
<?php
// no ending tag when only php code
if (isset($_POST["submit"])) {
//$ signifies a variable
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["uid"];
$pwd = $_POST["pwd"];
$pwdrepeat = $_POST["pwdrepeat"];
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if(emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) !== false) {
header("location: ../signup.php?error=emptyinput");
exit();
}
if(invalidUid($username) !== false) {
header("location: ../signup.php?error=invaliduid");
exit();
}
if(invalidEmail($email) !== false) {
header("location: ../signup.php?error=invalidemail");
exit();
}
if(pwdMatch($pwd, $pwdRepeat) !== false) {
header("location: ../signup.php?error=passwordsdontmatch");
exit();
}
if(Uidexists($conn, $username, $email) !== false) {
header("location: ../signup.php?error=usernametaken");
exit();
}
createUser($conn, $name, $email, $username, $pwd);
}
else {
header("location: ../signup.php");
exit();
}
This is the start of my functions.inc.php file where the undefined $result occurs.
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) {
$result;
if(empty($name) || empty($email) || empty($username) ||
empty($pwd) || empty($pwdrepeat)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
can you just remove the first $result in the function and check again.
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) {
if(empty($name) || empty($email) || empty($username) ||
empty($pwd) || empty($pwdrepeat)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
also you can do it shorthand
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) {
return (empty($name) || empty($email) || empty($username) ||
empty($pwd) || empty($pwdrepeat)) ?? false;
}
can anyone please tell me why when the results are equal then it echo's out the response, but when the results are false then it shows a blank page. I am sure i have gone wrong somewhere.
if(isset($_GET['EX1']) && $_GET['EX2'] != ""){
$Email = $_GET['EX1'];
$Password = $_GET['EX2'];
$userDetails = DB::table('applicants')
->where('Email', '=', $Email)
->where('Password','=', $Password)
->get();
if($Email = $userDetails[0]->Email && $Password = $userDetails[0]->Password){
echo 'Both are the same ';
}else{
echo 'Not the same';
}
}
if($Email = $userDetails[0]->Email && $Password = $userDetails[0]->Password){
should be
if($Email == $userDetails[0]->Email && $Password == $userDetails[0]->Password){
You're using assignment = instead of comparison == which will always return true
This would work!
public function myfunction(Request $request) {
if(!empty($request->EX1) && !empty($request->EX2)){
$Email = $request->EX1;
$Password = $request->EX2;
$userDetails = DB::table('applicants')
->where('Email', '=', $Email)
->where('Password','=', $Password)
->get();
if($Email == $userDetails[0]->Email && $Password == $userDetails[0]->Password){
return 'Both are the same ';
}else{
return 'Not the same';
}
} else {
return 'error';
}
}
Btw, you need to change if(.... = ....) to if(.... == ....). = is for assigning values and == is for comparing values.
Hope this works!
I've tried all the other answers for my query on here and none of them suited my needs. I'm on PHP 5.2, and I'm starting to think that maybe this filter doesn't work on my version? I've added code below, let me know if you see anything. I've tried it with and without sanitizing, and every time it just skips over the entire clause and submits the form to the database.
if(isset($_POST['fname']) && !empty($_POST['fname']) AND isset($_POST['lname']) && !empty($_POST['lname']) AND isset($_POST['college']) && !empty($_POST['college']) AND isset($_POST['address']) && !empty($_POST['address']) AND isset($_POST['username']) && !empty($_POST['username']) AND isset($_POST['password']) && !empty($_POST['password']) AND $passwordEmail == $password2){
$address = $_POST['address'];
$address = filter_var($address, FILTER_SANITIZE_EMAIL);
if(!filter_var($address, FILTER_VALIDATE_EMAIL) === false) {
header("location: registration.php?remarks=successEmail&college=$college&fname=$fname&lname=$lname&contact=$contact&username=$username");
}
else{
header("location: registration.php?remarks=failedEmail&college=$college&fname=$fname&lname=$lname&contact=$contact&username=$username");
}
Edit:
I removed the duplicate function and the ! and it still does not work.
The only addition was the moving of the '!' and the removal of the first filter.
This is the code I am now using which is still failing and taken from some of the answers given:
if(isset($_POST['fname']) && !empty($_POST['fname']) AND isset($_POST['lname']) && !empty($_POST['lname']) AND isset($_POST['college']) && !empty($_POST['college']) AND isset($_POST['address']) && !empty($_POST['address']) AND isset($_POST['username']) && !empty($_POST['username']) AND isset($_POST['password']) && !empty($_POST['password']) AND $passwordEmail == $password2){
$address = $_POST['address'];
if(filter_var($address, FILTER_VALIDATE_EMAIL) != false) {
header("location: registration.php?remarks=successEmail&college=$college&fname=$fname&lname=$lname&contact=$contact&username=$username");
}
else{
header("location: registration.php?remarks=failedEmail&college=$college&fname=$fname&lname=$lname&contact=$contact&username=$username");
}
Final Edit: I copied this example I found and just changed the variable names and now it works.
<?php
$email = "john.doe#example.com";
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
?>
Your ! is in the wrong place.
if(filter_var($address, FILTER_VALIDATE_EMAIL) !== false) {
By putting it before the function name you are saying "if the opposite of the return value of filter_var() is equal to false and the same type". That gives you true === false which of course is false.
What you really mean is "if the return value of filter_var() is equal to false and the same type". That gives you true !== false which is of course true.
Or simplify it:
if(filter_var($address, FILTER_VALIDATE_EMAIL)) {
You are using filter_var TWICE thats why it will always fail doing this
if(!filter_var($address, FILTER_VALIDATE_EMAIL) === false) {
The variable $address has already been modified to a boolean value in the line before:
$address = filter_var($address, FILTER_SANITIZE_EMAIL);
$address has the false or true value instead of the email string!
Oh, and of course, like John Conde said, you're using the ! in the wrong place.
I had the same problem. What I found out was that the else was below the }. When I moved it right behind the } the validation worked.
Wrong code:
if(filter_var($em, FILTER_VALIDATE_EMAIL)){
$em = filter_var($em, FILTER_VALIDATE_EMAIL);
}
else{
echo "Invalid email format";
}
Working code:
if(filter_var($em, FILTER_VALIDATE_EMAIL)){
$em = filter_var($em, FILTER_VALIDATE_EMAIL);
//Check if email already exists
}else{
echo "Invalid email format";
}
I created a very simple login page where I validate the username and password on PHP after submitting the form.
My first step in the validation is to check that the username and password are not empty, if empty I send the user back to the login page:
$e = $_POST['email'];
$p = $_POST['password'];
if($e == '' || $p == '' || is_null($e) || is_null($p)){
header('Location: login.php?e=missing');
}
it turns out that the if statement that checks if the username and password are empty only works if I add an else statement:
$e = $_POST['email'];
$p = $_POST['password'];
if($e == '' || $p == '' || is_null($e) || is_null($p)){
header('Location: login.php?e=missing');
}else{
//more validation
}
To be honest, my code works, I added the else and problem solved, but WHY???
Thanks.
$e = $_POST['email'];
$p = $_POST['password'];
if(trim($e) == '' || trim($p) == ''){
header('Location: login.php?e=missing');
exit(); // add this else it wont leave the page!
}else{
//more validation
}
if($e == '' || $p == ''){
header('Location: login.php?e=missing');
}
You need to add an exit() after the header line; otherwise, processing continues as normal and all the rest of the code is run.
you need to use empty to check if the variables are set, as well as empty.
$e = $_POST['email'];
$p = $_POST['password'];
if( empty($e) || empty($p) ) {
header('Location: login.php?e=missing');
exit;
}
else {
//more validation
}
$e = $_POST['email'];
$p = $_POST['password'];
if(!empty($e) && !empty($p))
{
header('Location: login.php?e=missing');
exit();
}
else
{
//more validation
}
if($e == '' || $p == ''){
header('Location: login.php?e=missing');
}
rathen then that use :
if(!isset($e) || !isset($p)){
header('Location: login.php?e=missing');
exit();
}
It is happens because of white space or you can use trim() function to remove white space and use exit(); after header its mandatory*
I basically took in 3 pieces of data from a form, and before processing them, I just wanted to make sure that all fields were filled in. So the focus of this is the second to last IF statement, checking if the different variables are empty. It seems to only be working for the first variable and I can't figure out how to make it apply to all of them.
<?php
include ("account.php") ;
include ("connect.php") ;
$isdone = FALSE;
$un = $_REQUEST [ "un"] ;
$pw = $_REQUEST [ "pw"] ;
$data = mysql_query("SELECT * FROM `auth` WHERE username = '$un'") or die(mysql_error());
$info = mysql_fetch_array($data);
$info['username'];
$password = $info['pw'];
session_start();
if(trim($un) != '' && trim($pw) != '' && $password == $pw)
{
$_SESSION['uze']=$un;
include "problem.html";
}
elseif( !isset($_POST['submit1']) && $isdone == FALSE)
{
echo "wrong password";
}
$selected = $_REQUEST [ "type"] ;
if($selected == 'afs')
{
$typeinc = 'afs';
}
else if($selected == 'db')
{
$typeinc = 'database';
}
else if($selected == 'cs')
{
$typeinc = 'computer systems';
}
else if($selected == 'pw')
{
$typeinc = 'password';
}
else if($selected == 'hw')
{
$typeinc = 'hardware';
}
else if($selected == 'other')
{
$typeinc = 'other';
}
$text = $_REQUEST ["inc"];
$selected2 = $_REQUEST ["yesno"];
if($selected2 == 'yes')
{
$email = 'yes';
}
else
{
$email = 'no';
}
if(isset($_POST['submit1']))
{
if(empty($typeinc) || empty($text) || empty($email))
{
print( 'You have not filled in all fields, click to sign in and re-enter' );
}
}
else{
mysql_query("INSERT INTO `swp5_proj`. `inci` (`type`, `date`, `time`, `reporter`, `desc`) VALUES ('$typeinc', CURDATE(), CURTIME(), '".$_SESSION['uze']."', '$text');") or die(mysql_error());
mysql_query("DELETE FROM inci WHERE type = ' '");
$isdone = TRUE;
}
if(isset($_POST['submit1']) && $isdone == TRUE)
{
echo "session over";
}
?>
Make sure you clean your REQUEST variables before you put them in a MySQL query.
if((trim($un) !== '') && (trim($pw) !== '') && ($password == $pw))
You're setting $email to yes or no in the line just above.
In your if statement you are using the shortcut OR operator.... As soon as a single statement evaluates to true, the entire statement evaluates to true and there is no need to continue processing further.