tags:

views:

46

answers:

2

This is my script:

session_start();
include "../inc/conn.php";
if ($_GET['login']=="yes") { 
 echo 'test2';
$username=$_POST('username');
$password=$_POST('password');
echo $username.' '.$password;
$userq=mysql_query("SELECT * FROM members WHERE username='$username' AND password='$password'") or die(mysql_error());
if (mysql_num_rows($userq)=="1") { $_SESSION['chkuser']="confirmed"; $_SESSION['username']=$user; }
else { echo 'Потребителското име и/или паролата са грешни. Моля опитайте отново.'; }
}
echo $user.' '.$pass;
if ($_SESSION['chkuser'] <> "confirmed") {
echo '<div align="center"><strong>Моля въведете име и парола</strong>:<br/><br/><br/>';
?>
<form name="form1" method="post" action="?login=yes">
  Потребител: <input type="text" name="username"  />
Парола: <input type="password" name="password" /><br/><br/>
    <p><input type="submit" name="Submit" value="Вход" /></p>
  </div>
</form>
<?
exit();
}

Test 2 is echoed, but username and password are not sent via POST - scripts breaks after $_POST is used.. Do you guys see where my error is ?

+6  A: 

Use [], not () to get data from $_POST array:

$username=$_POST['username'];
$password=$_POST['password'];

Also, I might mention that you should escape user input before using it in mysql query:

$username=mysql_escape_string($_POST['username']);
$password=mysql_escape_string($_POST['password']);

If you don't do this, anyone can insert something like this:

username=admin
password=blabbla' OR '1'='1

and will login as admin without knowing the password :)

Silver Light
`$_POST('username');` what this actually does is call a function by the name that is the value of `$_POST` (which is an array, so it's probably an empty string in this case). These are called [variable functions](http://www.php.net/manual/en/functions.variable-functions.php).
Core Xii
@Core Xii, yes. But I doubt that the original intention was to use variable function :)
Silver Light
@Silver Light: I know. Just letting him know what his code actually did, as opposed to what it was supposed to do. (since it was technically correct syntax)
Core Xii
+4  A: 

Shouldn't this:

$username=$_POST('username');
$password=$_POST('password');

be

$username=$_POST['username'];
$password=$_POST['password'];

?

Adhip Gupta
darn it, I'm an idiot.. thanks
DreamWave
Don't forget to accept an answer! :)
Drewdin