Trying to make an ajax login I found
here work with cookies.
Problem is the cookie isn't setting.
Ajax
CODE
<script language="javascript" type="text/javascript">
var phpscript = 'login.php';
function createRequestObject() {
var req;
if(window.XMLHttpRequest){
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
req = new ActiveXObject("MS.XMLHTTP");
} else {
alert('There was a problem creating the XMLHttpRequest object');
}
return req;
}
var http = createRequestObject();
function sendRequestPost() {
var user = document.getElementById('username').value;
var pass = document.getElementById('password').value;
http.open('post', phpscript);
http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http.onreadystatechange = handleResponsePost;
http.send('username='+ user +'&password='+ pass);
}
function handleResponsePost() {
if(http.readyState == 1){
document.getElementById("response").innerHTML = "Processing login..";
} else if(http.readyState == 4 && http.status == 200){
var response = http.responseText;
if(response) {
document.getElementById("response").innerHTML = response;
}
}
}
</SCRIPT>
Login.php
CODE
<?php
//mysql connect is up hurr
ob_start();
$username = $_POST['username'];
$password = $_POST['password'];
$query = mysql_query("SELECT * FROM users WHERE username='". $username ."' AND password='". $password ."'");
$result = mysql_num_rows($query);
$row = mysql_fetch_array($query);
$blah = $row["username"];
if(isset($_COOKIE[$blah])){
if ($result == 1){
$_POST["username"] = $row["username"];
setcookie("username", $row["username"],time()+(60*60*24*5), "/", "");
setcookie("password", $row["password"],time()+(60*60*24*5), "/", "");
echo 'Logged in, '. $row["username"];
}else{
echo 'Wrong username or password.';
}
}else{
echo("<DIV id=\"response\">
<form method=\"post\" action=\"\">
<input name=\"username\" value=\"Username\" type=\"text\" name=\"username\" id=\"username\" style=\"margin-bottom: 1px;\" onfocus=\"this.value=''\">
<input name=\"password\" value=\"******\" type=\"password\" name =\"password\" id=\"password\" style=\"margin-bottom: 1px;\" onfocus=\"this.value=''\">
<input name=\"button\" type=\"button\" id=\"submit\" onClick=\"sendRequestPost();\" name=\"submit\" value=\"Login\" />
</form></DIV>");
}
?>
Been messing with this for hours, so I think I'm just missing something really obvious.
I'm not looking for some copy and paste code so itll work, just an explanation on what I'm doing wrong. XS may not be the best place to post this, but I think someone here will be able to help me out.