JC99 Posted July 6, 2009 Share Posted July 6, 2009 Hello everyone, I am trying to create a PHP login script using cookies but am having some troubles. Basically here is my setup index.php -> login.php -> admin.php I want a form on the index.php script that has the username and password and passes the $_POST['username'] and $_POST['password'] variables to login.php Then login.php authenticates against a database of allowed users (Which I already is setup and works fine) and also (and I am not sure about this) sets the cookie info from the form. Finally admin.php is loaded. The admin.php code would look like the following.. <?php if (isset($_COOKIE['username']) && isset($_COOKIE['password']) { echo "success!"; } else { echo "Failure"; } ?>?> So basically I need to somehow create a cookie from index.php and pass the information to login.php and then pass the information to admin.php. Anyone know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/164894-help-creating-a-simple-php-login-with-cookies/ Share on other sites More sharing options...
RussellReal Posted July 6, 2009 Share Posted July 6, 2009 setcookie Quote Link to comment https://forums.phpfreaks.com/topic/164894-help-creating-a-simple-php-login-with-cookies/#findComment-869538 Share on other sites More sharing options...
JC99 Posted July 6, 2009 Author Share Posted July 6, 2009 index.php -> login.php -> admin.php Yeah, I know about that. But I am obviously using it the wrong way cause its not working.I set the cookie like this... setcookie("Admin", $username); Do I set the cookie on index.php (where the login form is) or login.php which the forms loads when my username and password are submitted. And if I set it on index.php, how do I access it on admin.php? Just a note: The login.php just checks that the user is a valid by authenticating with the database and then, if everything is valid, uses the following code to load admin.php. header("location:admin.php"); Quote Link to comment https://forums.phpfreaks.com/topic/164894-help-creating-a-simple-php-login-with-cookies/#findComment-869547 Share on other sites More sharing options...
JC99 Posted July 6, 2009 Author Share Posted July 6, 2009 I finally got it working. I needed to setcookie() in login.php. Also, the names of the cookies where wrong(They where Admin when they should have been adminuser and adminpass) Once I fixed that then the following worked in admin.php... <?php if (isset($_COOKIE['adminuser']) && isset($_COOKIE['adminpass'])) { echo "Success"; } else { echo "Failed"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/164894-help-creating-a-simple-php-login-with-cookies/#findComment-869607 Share on other sites More sharing options...
Adam Posted July 6, 2009 Share Posted July 6, 2009 Although the script may work, it isn't exactly secure. Consider that cookies can be added / modified so easily, just testing if the cookie has been set isn't secure enough, because they can just add it. There's also the chance of cookie stealing scripts obtaining your login info; you'd be better off using PHP sessions. Quote Link to comment https://forums.phpfreaks.com/topic/164894-help-creating-a-simple-php-login-with-cookies/#findComment-869609 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.