0

Here is the PHP block i am using:

$reg = @$_POST['reg'];

$fn = "";
$ln = "";
$un = "";
$em = "";
$em2 = "";
$pswd = "";
$pswd2 = "";
$d = "";
$u_check = "";

$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$em2 = strip_tags(@$_POST['email2']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$d = date("Y-m-d"); //Year - Month - Day

if ($reg) {}
if ($em==$em2){}
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
$check = mysql_num_rows ($u_check);
if ($check == 0){}
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2){}
if ($pswd==$pswd2){}
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25){
echo "The maximum amount of character is 25! Please try again";
}
else
{
if (strlen($pswd)>30||strlen($pswd)<5){}
echo "Your password be between 5 and 30 characters long!";
}
else
{
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES (' ', '$un', '$fn', '$ln',   
'$em','$pswd','$d','0')");
}

And here is the two else statements:

else
{
if (strlen($pswd)>30||strlen($pswd)<5){}
echo "Your password be between 5 and 30 characters long!";
}
else
{
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES (' ', '$un', '$fn', '$ln',   
'$em','$pswd','$d','0')");
}

When i only have:

else
{
if (strlen($pswd)>30||strlen($pswd)<5){}
echo "Your password be between 5 and 30 characters long!";
}

I don't get an error message, but when i add:

else
{
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysql_query("INSERT INTO users VALUES (' ', '$un', '$fn', '$ln', '$em','$pswd','$d','0')");
}

I get this error message when i refresh:

Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\Socially\index.php on line 39

I am using a YouTube tutorial, and this is what he typed, he didn't get an error message. Here is the link: https://www.youtube.com/watch?v=EgqVNMTnmDQ&list=PLA7F9875BD031DC16&index=36

This video was done in 2013.

If someone could help me, it would be appreciated.

chris85
  • 23,846
  • 7
  • 34
  • 51
adam hope
  • 3
  • 6

1 Answers1

1

Your ifs are wrong the { opens the control block and the } closes it. For example with this:

if (strlen($pswd)>30||strlen($pswd)<5){}

You are doing nothing when the password is longer than 30 characters or less than 5. (also why limit passwords to 30 characters?)

You also then are echoing the message regardless of that condition:

echo "Your password be between 5 and 30 characters long!";

Additional notes:

Your control blocks should be indented.

You should NOT put user data directly into a SQL query. That is how injections occur. strip_tags does nothing to stop a SQL injection.

Try:

<?php
$reg = @$_POST['reg'];
$fn = "";
$ln = "";
$un = "";
$em = "";
$em2 = "";
$pswd = "";
$pswd2 = "";
$d = "";
$u_check = "";
$fn = strip_tags(@$_POST['fname']);//dont use @, no need for error supression, resolve the errors.
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$em2 = strip_tags(@$_POST['email2']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$d = date("Y-m-d"); //Year - Month - Day
if ($reg) {}//does nothing
if ($em==$em2){}//does nothing
$u_check = mysql_query("SELECT username FROM users WHERE username='$un'");
$check = mysql_num_rows ($u_check);
if ($check == 0){}//does nothing
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2){}//does nothing
if ($pswd==$pswd2){}//does nothing
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25){
    echo "The maximum amount of character is 25! Please try again";
} else {
    if (strlen($pswd)>30||strlen($pswd)<5){
        echo "Your password be between 5 and 30 characters long!";
    } else {
        $pswd = md5($pswd); //should upgrade hashing algorithm
        $pswd2 = md5($pswd2);//not used
        $query = mysql_query("INSERT INTO users VALUES (' ', '$un', '$fn', '$ln',   
        '$em','$pswd','$d','0')");//open to SQL injections
    }
}
chris85
  • 23,846
  • 7
  • 34
  • 51