Click to See Complete Forum and Search --> : javascript with php - cookies


meme
05-29-2005, 07:46 AM
Hi all the fine developers out there,

I am trying to develop a website using PHP and Javascripting. I have 2 questions, I will post the second one in a seperate thread.

This question is about cookies. I tried to set a cookie on a user name on one page and then retrieve it on the next page. However I get the cookie displayed as:

Cookie name is 'PHPSESSID', and the value is '39f842f3b9a235bea4e1aad5dfa7411e'

Here is my code:

in the firstpage where I am setting the cookie:
expireDate = new Date
expireDate.setMonth(expireDate.getMonth()+6)

userName = ""
if (document.cookie != "") {
userName = document.cookie.split("=")[1]
}

function setCookie() {
userName = document.loginform.nameField.value
document.cookie = "userName="+enter_username+";expires=" + expireDate.toGMTString()
}



<form name="loginform" action="resultpage.php" method=post onsubmit= "return validForm(this)">
<h1>enter your login name: <input type=text name="enter_username" onblur="setCookie()"></h1>
<input type=submit value="login">
</form>

-------------
AND ON THE NEXT PAGE (RESULTPAGE), here is the code to retrieve the information:

if (document.cookie == "") {
document.write("There are no cookies here")
}
else {
thisCookie = document.cookie.split("; ")

for (i=0; i<thisCookie.length; i++) {
document.write("Cookie name is '"+thisCookie[i].split("=")[0])
document.write("', and the value is '"+thisCookie[i].split("=")[1]+"'<BR>")
}
}

I would really appreciate your prompt response.

Meme

Scleppel
05-29-2005, 11:27 AM
Are you using PHP sessions? because the PHPSESSID cookie is set by PHP to store the session ID. You can't access $_SESSION values in javascript.

meme
05-29-2005, 01:53 PM
I am using PHP sessions; how is this relevant?
Am I suppose to do something else?


cheeers

Scleppel
05-29-2005, 02:02 PM
Are you trying to access the data stored in the sessions through javascript? If so, I don't think you can.

meme
05-29-2005, 02:19 PM
No, my php sessions are not used for cookies.
I am trying to use only javascript for cookies, but my page has other php stuff.

thanx

Scleppel
05-29-2005, 03:24 PM
I think you should ask in the javascript forum. I tried you javascript, it didn't set a cookie at all :confused: and there's probably a way of finding onyl the cookie you want, not looping through and finding all cookies (because that will show up the PHP session cookie).