Forum Moderators: coopster
I'm trying to direct the user to one of two pages based upon whether they supply valid log-in credentials or not. The code is pretty simple, but I've posted it below anyway.
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$id=$_POST['id'];
$passwd=$_POST['passwd'];
$query="SELECT * FROM useraccounts WHERE id='$id' and passwd='$passwd'";
$result=mysql_query($query);
$numrows=mysql_numrows($result);
mysql_close();
IF ($numrows==0){
//echo "Invalid Username or Password";
header('location: failedlogin.html');
}else{
//echo "Thank you, " . $id . "!";
header('location: success.html');
}
?>
I know that the conditioning works because of the echo statements that have since been commented out, but I'm just having a bit of trouble finding the command that will automatically send the user to the appropriate page based upon the result of the query.
Any help is much appreciated, and thanks in advance.
To answer your first question, no, I'm not getting sent anywhere when either of the conditions are true, just getting the error message "Parse error: parse error, unexpected T_STRING". That's what I was hoping to fix :) Was I anywhere close to having the right command when I put in "header('location..."?
Regarding your other suggestions/fixes, both ID and password are varchar, but how would I go about checking to see if the "posted values are the expected ones"?
I have also inserted the 'mysql_real_escape_string' per your suggestion; thanks for that.
Again, sorry to be asking such rudimentary questions, but I'm basically relying on online tutorials and forums such as this for support, as I was thrown into this project head-first without any knowledge of php whatsoever.
I look forward to your response!
Since password must be in most cases alpha numerical you may use the same regular expression.
Checking for alpha only then remove 0-9
Allowing for more like punctuation and spaces and dash
if(!preg_match('/^[a-z0-9\-\,\.\ ]*$/i', $id))
etc … got the idea...
Turns out that my issue was a blank line, as PHP apparently interprets those as echo statements? Anyway, I learned that I couldn't have any blank spaces before my header statement, so I took those out and everything worked!
Thanks again for your help, and I'm sure that you'll be hearing from me again in the near future.
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$id=$_POST['id'];
$passwd=$_POST['passwd'];
$query="SELECT * FROM useraccounts WHERE id='$id' and passwd='$passwd'";
$result=mysql_query($query);
$numrows=mysql_numrows($result);
mysql_close();
IF ($numrows==0){
//echo "Invalid Username or Password";
header('location: failedlogin.html');
}else{
//echo "Thank you, " . $id . "!";
header('location: success.html');
}
?>
1.) Variables within single quotes should NOT be expanded by PHP.
"Unlike the two other syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings."
[us.php.net...]
(Yes, they were not technically variables within '', because the '' were within a "" string, but it's good practice... I thought it should break with the localhost not being a string...)
2.) localhost SHOULD be a string. IOW 'localhost'
[us.php.net...]
Along with what the other posters have said, to me it looks like your quoting patterns are missing or backward...
<?php
include('dbinfo.inc.php');
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( 'Unable to select database');
$id=$_POST['id'];
$passwd=$_POST['passwd'];
$query="SELECT * FROM useraccounts WHERE id='".$id."' and passwd='".$passwd."'";
$result=mysql_query($query);
$numrows=mysql_numrows($result);
mysql_close();
if ($numrows==0){
//echo "Invalid Username or Password";
header('location: failedlogin.html');
}else{
//echo "Thank you, " . $id . "!";
header('location: success.html');
}
?>
The problem you were having with the header was NOT a blank line... (I use them often before setting headers.) It was most likely because you had an error and output was sent to the browser before you tried to set the header. A blank line is NOT equal to echo, so there was another issue... My guess from the error you quoted, is it was not having the localhost quoted.