Jump to content

trouble setting cookies


duncan_cowan

Recommended Posts

hi i have made a script that is meant to process a login and it used to run with sessions but i wanted to use cookies to carry the loggin info so i set it to set some cookies but it doesnt work...please could someone help?script:

$username = $_POST['uname'];	$password = $_POST['passwd'];	setcookie("username", "$username");	setcookie("password", "$password");

thanks in advance... :)

Link to comment
Share on other sites

OK. Either you are setting the cookie wrong or checking it wrong. Keep in mind that you cannot check a cookie on the same page that you set it. Cookies get sent by the browser when it asks for the page, so by the time the PHP starts all cookies have already been sent. So you have to set the cookie on one page, and then have a link on the page to another page that checks the cookie. It's also not a good idea to redirect after you set a cookie, some browsers will ignore the cookie and just redirect. So you want to set the cookie, and then show some HTML on the page. You also can't set cookies after you send any HTML output, cookies need to be set before you output any HTML.Check the reference page for setcookie to see other parameters for it, like the path and domain. Scroll down the page to see code examples, and scroll down farther to see comments and examples from other people.http://www.php.net/manual/en/function.setcookie.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...