Okay, so my website admin finally got back to me and I can now use my database. I just attempted to make a functioning Login, but with no success.
The issue seems to be the fact that the action tag in form (<form action="process.php">) opens the php script in another tab. http://www.thefunnyzone.co.uk
Username: Jon Password: password.
It sounds like you might have a target attribute set in the form tag. If this exists and is anything other than _self or _top then it may well be opening in a new tab.
Your HTML form contains no input element named 'username'.
User-supplied data should never be placed directly into a SQL query string, else your code will be vulnerable to SQL injection attacks and/or just plain SQL errors. Instead, you must first sanitize the data (such as by using mysqli_real_escape_string() for string data) or by using prepared statements.
Your PHP code is always attempting to execute the SQL query and check for results - even if it's the first time the user has loaded the form and hasn't even submitted any login credentials yet.
You never check to see if the SQL query failed to execute before attempting to use the result set.
Don't use 'SELECT *' - instead, only SELECT the information that you actually need. Since in your case you don't really need information from the database (you just want to check if there is a row that matches the WHERE criteria), you could instead SELECT some constant value (e.g. do a 'SELECT 1').
PHP code won't get executed if it's placed inside a string, so unless you meant to display to your user the word "Welcome" followed by a comma, a space, a less-than symbol, a question mark, ... etc., then you should either use concatenation or variable interpolation here instead. See the manual page for string for more explanation/examples.
Your current code doesn't do anything if the username and password are incorrect. Wouldn't it at least be helpful to the user if you output some error message if no matching row can be found in your DB? (Note you'll want to make sure you address issue #4 above first, otherwise you'll be saying the user's username/password combination is invalid before they've even given you one.)
Last edited by bradgrafelman; 12-12-2012 at 01:50 PM.
Don't ever rely on Javascript validation, ever, ever, ever! Javascript can be turned off or be unavailable, and all users are evil bastards who want to destroy your server. Any kind of validation always needs to be done server side too, as that's the only area where you have full control.
<?
$mysqli = new mysqli("", "", "", "");
if ($mysqli->connect_errno > 0) {
# what version of PHP are you running?
# mysqli::connect_error() was broken before 5.2.9
die('Unable to connect to database [' . $mysqli->connect_error . ']');
}
$username = $mysqli->real_escape_string($_POST['user']);
# don't hash the escaped password.
# actually, since md5 hashes never have characters that need to be escaped,
# you can skip it if you like.
# others here might point out that md5 is considered "broken" for security purposes nowadays.
$password = md5($mysqli->real_escape_string($_POST['password']));
# why two queries? why not just one, and see if it's empty or not?
$mysql->query("SELECT `id` FROM `Users` WHERE `Username` = '$username' && `Password` = '$password'");
# this WON'T WORK.
// $sql = <<<SQL SELECT * FROM `Users` WHERE `Username` = $username && `Password` = $password SQL;
# a heredoc needs to be like this:
$sql = <<< SQL
SELECT * FROM `Users` WHERE `Username` = '$username' && `Password` = '$password'
SQL
;
# explanation:
# opening token needs to be on its own line.
# likewise with the closing token,
# which also must NOT be indented or have any other characters (even whitespace) on the same line
# (technically, the closing ; can *sometimes* be included,
# but for simplicity I just always put it on the following line).
# also note I added 'quotes' to the string values in your SQL.
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
# this should be else*if*
}else($result->num_rows==1){
# but personally I'd change the whole approach:
# if( num rows === 1){ good }elseif( error ){ ugly }else{ bad }
$expires = 1 * 1000 * 60 * 60 * 24 * 2;
setcookie("username", $username, time()+$expires);
# why two cookies?
# WHY SAVE THE HASH IN A COOKIE? (bad security!)
setcookie("password", $password, time()+$expires);
echo '<strong>Welcome,'.$username.'!</strong>';
};
$mysqli->close();
The website is now working properly (don't use ie), but I'm having the same problem as before, the form submit button opens the file instead of running it. http://www.thefunnyzone.co.uk
The website is now working properly (don't use ie), but I'm having the same problem as before, the form submit button opens the file instead of running it. http://www.thefunnyzone.co.uk
The login form on that URL is just redirecting to a blank index.php file. Either you've got an error that's set to not display, or it really is blank. Nothing seems to be opening in a new tab though.
Bookmarks