Hello all,
I am brand new to PHP and MySQL. I have created my login page and necessary php files to check the password and authenticate. If I execute my script directly with PHP, i.e. C:\php>php.exe check_login.php, then the connection to MySQL db is successful, but when I am using my login page from my web server, then it doesn't work. (I can't even get the log where to trace and see the error being thrown).
Here's what I have done:
- Created MySQL table "users" and have an entry in it.
- Created login.php as follow:
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="check_login.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername">
</td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
- created check_login.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="admin"; // Mysql password
$db_name="wsc"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
// username and password sent from form
$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);
$count=mysql_num_rows($result);
if($count==1){
session_register($myusername);
session_register($mypassword);
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
- created login_success.php
<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
- created [b]logout.php[/b]
[code]
<?
session_start();
session_destroy();
?>
When I enter my user name/password, then it gets redirected to check_login.php, but i see a blank screen. If it's successful, i should see "Log in Successful" message, as I have in my login_success.php.
When I execute this script directly from php as follow: (C:\php\php.exe test.php) then I get success message.
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="admin"; // Mysql password
$db_name="wsc"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select database.
mysql_connect($host, $username, $password)or die("cannot connect");
echo "Connected";
mysql_select_db($db_name)or die("cannot select DB");
echo "DB selected";
// username and password sent from form
$myusername="wsc_user";
$mypassword="wsc";
// 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 . "'";
echo $sql;
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
echo "sucess";
session_register($myusername);
session_register($mypassword);
//header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
Can someone please help? Really appreciate it.