login form code snippet from html file
<form action="/phpScripts/login.php" method="post" onsubmit="getCookie()">
User/E-mail <input id="loginname" type="text" name="uid" class="inpun"><br>
Password <input id="loginpass" type="text" name="pass" class="inppass"><br>
<input type="submit" name="login" value="login" ><br>
</form>
getCookie function code from js file
function getCookie()
{
var user=document.getElementById('loginname').value;
var expires=new Date();
expires.setFullYear(expires.getFullYear()+1);
document.cookie=encodeURIComponent("name")+ "="+encodeURIComponent(user)+ "; expires=" + expires.toTimeString()+ "; domain=.xyz.com";
alert(document.cookie);
}
login.php (form action) do the database query stuff and checks for authentication and if all is well it stores session variables(username in the login form) and redirect to the home page. I want to store username in javascript cookies also so that i can load user specific data like users profile pic from the folders created in the root document directory at the time of registering (signup) for example:-
$(document).ready(function(){
var usr=document.cookie;
/*location=url(data/abc/abc.jpg)*/
location="url(data/" + usr + "/" + usr +".jpg" + ")";
$(div#profilepic).css({'background-image':location, 'width':'180px', 'height': '180px'});
});
please correct me if I am wrong in the jquery code above. Also is there any way to store php $_SESSION data in javascript variable?