Hooch Posted May 24, 2006 Share Posted May 24, 2006 Hey all. I'm trying to add a cookie session to my script. For my cookie I will use: setcookie('panelname', 'admin', time()+36000); This is setting $panelname to admin (I think anyway)Here in my existing code I have a login script. (on the page I want to get in to)If I log in with the set username and pass It is successfull.But everytime I go back I have to log in. I'm having alot of trouble finding out where I put the setcookie. <?php//add the databaseinclude 'db.php'; // Define your username and password$panelname = "admin";$password = "pass";if ($_POST['txtUsername'] != $panelname || $_POST['txtPassword'] != $password) {?><h1>Login</h1><form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <br /><label for="txtUsername" class="main">Username:</label><br /><input type="text" title="Enter your Username" name="txtUsername" class="txtbox" /><br /><label for="txtpassword" class="main">Password:</label><br /><input type="password" title="Enter your password" name="txtPassword" class="txtbox" /><br /><input type="submit" name="Submit" value="Login" /></form><?php } else {?> <table>WEBPAGE HERE</table><?php }mysql_close();?> I hope I am clear enough. Thank you, Hooch Link to comment Share on other sites More sharing options...
justsomeguy Posted May 24, 2006 Share Posted May 24, 2006 If you create a cookie called "panelname", that doesn't mean the variable $panelname automatically gets the cookie value in every page. You can find the value of the cookie in $_COOKIE['panelname'].Was that your question? Link to comment Share on other sites More sharing options...
Hooch Posted May 24, 2006 Author Share Posted May 24, 2006 I want to stop having to log in everytime I go back to this page.This page is an admin type page where I click links to set up other parts.Once the part is set up, I then go back to this original admin page.But everytime I go back I have to log in. Any thing I have tried has brought me errors. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 25, 2006 Share Posted May 25, 2006 If you want to create a login page that checks for the cookie and logs you in automatically, you can do that. You can have your normal login code, but before that, check if the username is set in the $_COOKIE array, and if it is, set it in the session and redirect the user away from the login page. To set that cookie, if you want to make it last for a year or something do:setcookie("CookieName", $value, time()+60*60*24*365, "/", ".mysite.com", 0); Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now