I'm probably doing something really stupid but I can't figure it out.
Code inside of main page
<script>
var pass = prompt("Enter pass:");
$.post(
"/php/sessions/set_session.php",
{ pass: pass },
function(result) {
if(result == "true")
{
window.location = "/Article_View/";
}
else
{
alert("Wrong password");
}
}
);
</script>
PHP Code
<?php
if($_POST["pass"] == "password")
{
//Vars for connecting to database.
$hostname = "secret";
$username = "secret";
$dbname = "secret";
//Login vars
$password = "secret";
$usertable = "secret";
//Connecting to database
mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);
//set IDstring cookie for this sesh
$expire = time()+60*60*24;//24hr cookie
$IDstring = rand_string(10);
setcookie("IDstring", $IDstring, $expire);
//save IDstring to SQL for comparasent of cookie
$query = "UPDATE {$usertable} SET IDstring='{$IDstring}'";
$result = mysql_query($query);
echo "true";
}
else
echo "false";
//IDstring gen
function rand_string( $length ) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ ) {
$str .= $chars[ rand( 0, $size - 1 ) ];
}
return $str;
}
?>
I can see that it sets the IDstring in phpmyadmin, but the cookie isn't setting at all.
I'm using Chrome and cookies are enabled. It don't show up in resources and isset($_COOKIE["IDstring"]) returns false.
What am I doing wrong?
Thanks in advance.