Login freezes at matching username / password

I am running on PHP5 and just cannot get this script to run past the user validation… set error messages ever step along the way but none are triggered, the script merely stops and returns a blank screen.

I am thinking that theres either something fishy with the SQL query syntax (not sure about username=’$myusername’ ) but I also am suspicious of the header location at the end. I played around with both but no result.

Any suggestions?
Thanks,
A2k

[php]

<?php ob_start(); $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Define $myusername and $mypassword $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql) or die("error sending the query"); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?>

[/php]

add this to the beginning of your script(s) when in development

[php]error_reporting(E_ALL);
ini_set(“display_errors”, 1);[/php]

[hr]

You should use parameterized queries in mysqli or pdo (they are safe) instead of the deprecated mysql_ library (not safe)

[hr]

You should not store passwords in the database, PHP has built in functionality to create safe hashes
http://php.net/manual/en/function.password-hash.php
http://php.net/manual/en/function.password-verify.php

[hr]

Do you call session_start() at the beginning of your script(s)?

Ah, thanks, turns out the session_register(“myusername”); tag is depreciated since PHP 5.3.

I know that MySQLi would be safer, but just dont have time to deal with rewriting the syntax now.

Thanks,
A2k

Sponsor our Newsletter | Privacy Policy | Terms of Service