Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP AJAX and COOKIES 1

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
US
Ok, I'm not really even sure where to post this JavaScript or PHP - I guess since the problem is in the PHP portion of the code I'll post it here.

I'm developing a PHP/AJAX (using the SAJAX php module *modified*) to create the user interface for a website.

The first thing a user is to do is to login - which works (the form calls the javascript which calls the php login function and returns either "Wrong" or "OK").

My problem is arising when I want to create a remember me box.

I've done the "Remember Me" thing before without using AJAX, and the cookies worked just fine - however with the login no utilizing AJAX the script is failing at the setcookie call.

If more info is needed just let me know and I'll provide what I can.

Has anyone dealt with this or know how I might get around this?
 
I wish I could give an error message, but that's the problem I'm not getting any it just stops.

Here is the end of the DoLogin($username, $password, $set_cookie = false) function.

Code:
		if($set_cookie === true){
			include('enc.php');
			$enc = new EnDecryptText();
			try{
				setcookie("Username", $enc->Encrypt_Text($user), time()+60*60*24*30, "/");
				setcookie("Password", $enc->Encrypt_Text($pass), time()+60*60*24*30, "/");
			}catch(Exception $e){
				return $e->getMessage();
			}
		}
		return 'OK';
I put the try...catch block in there to hopefully return an error message that it might be getting, but to no avail.

I have place a return with various strings at various points in this - before the setcookie calls, between them and after them. When put right before the setcookie calls I do receive the returned message, but between them I never receive the message.

I'm not quite sure what is going on.
 
The DoLogin function is called from an AJAX call so while there is output, it was output well before DoLogin call. Here's the form:

Code:
	<form id="login_form" action="" method="post" onsubmit="do_login(); return false;">
		<div id="form_content">
			<div class="message">
				Login Here
			</div>
			<div id="errTxt"></div>
			<div class="fields">
		        <label for="username" class="lin">Username:</label>
        		<input type="text" name="username" id="username" size="18" value="" /><br />
        		<label for="password" class="lin">Password:</label>
        		<input type="password" name="password" id="password" size="18" value="" /><br />
        		<input type="checkbox" name="PersistentCookie" value="yes" id="chkRemb" />
        		<label for="chkRemb">Remember me on this computer.</label><br />
        		<input type="submit" value="Sign in" id="cmdSubmit" /><br />
        	</div>
		</div>
	</form>

The do_login() call in the onsubmit handler pulls the values of the fields in the form and submits it to the php script, the callback function fills the div with the id errTxt with the returned value, which is how I saw the returned value when I placed it where-ever in the do login php script.
 
setcookie() must, of course, manipulate HTTP headers. All HTTP headers must precede any content-output.

You're probably getting a "Headers already sent" error. You could reconfigure PHP to send all error messages to a file: see the "error_log" runtime configuration directive in php.ini



Want the best answers? Ask the best questions! TANSTAAFL!
 
Yup, that was indeed the case.

Once I found that out from the log file, I found the problem easily in the Sajax.php file (came with the Sajax package, so I didn't create it), and the script echoed out "+:" _right_ before it called my function (which it stored in a variable), so I just moved it to after the function call.

Thanks sleipnir214!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top