Ok, so far I have always used JSP and Servlets for my web programming, but now I needed to make use of PHP... and I'm stuck at something really basic: setcookie.
The form works fine, and it checks with the database for the username/pass ok (because when I supply good credentials I return back to the homepage, and with bad credentials it appends the error parameter with the URL).
Anyway, the cookie is never set. The browser never receives a cookie. This is my login.php.
Thanks a lot! Really appreciate your help at this.
The form works fine, and it checks with the database for the username/pass ok (because when I supply good credentials I return back to the homepage, and with bad credentials it appends the error parameter with the URL).
Anyway, the cookie is never set. The browser never receives a cookie. This is my login.php.
Code:
<?php
$host="*********"; // Host name
$username="****"; // Mysql username
$password="***"; // Mysql password
$db_name="************"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("ERROR: Cannot connect to MySQL Server on 'localhost'.<br/><br/><i>Remember this is only a prototype demo!<br/>Franco</i>");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['uname'];
$mypassword=$_POST['pass'];
// 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);
// 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){
//Create cookie
$hour = time()+3600;
setcookie('ID_group_planner', $_POST['myusername'], $hour);
setcookie('Key_group_planner', $_POST['mypassword'], $hour);
header("location:index.php");
}
else {
header("location:index.php?login_error=1");
}
?>
Comment