Cr0w1 Posted September 14, 2010 Share Posted September 14, 2010 Hello A few days back I searched for a (very) easy php login script, basicly just to show hidden content that's not for everyone. This is the script I found (and it works): <?php $username = "Username"; $password = "Password"; $randomword = "one"; if (isset($_COOKIE['MyLoginPage'])) { if ($_COOKIE['MyLoginPage'] == md5($password.$randomword)) { ?> <?php include 'projecten_.php'; $name = "0"; ?> <?php exit; } else { echo "<p>Bad cookie. Clear please clear them out and try to login again.</p>"; exit; } } if (isset($_GET['p']) && $_GET['p'] == "login") { if ($_POST['name'] != $username) { echo "<p>Sorry, that username does not match. Use your browser back button to go back and try again.</p>"; exit; } else if ($_POST['pass'] != $password) { echo "<p>Sorry, that password does not match. Use your browser back button to go back and try again.</p>"; exit; } else if ($_POST['name'] == $username && $_POST['pass'] == $password) { setcookie('MyLoginPage', md5($_POST['pass'].$randomword)); header("Location: $_SERVER[php_SELF]"); } else { echo "<p>Sorry, you could not be logged in at this time. Refresh the page and try again.</p>"; } } ?> <body style="background:url(images/repeat.gif) repeat-x #0a0a09; height:106px; margin:0px; padding:0px; text-align: center;"> <div style="margin-left: auto; margin-right: auto; position: relative; top: 120px; width: 250px; text-align: left; line-height: 28px;"> <img src="images/zenze.png" alt="Zenze" style="position: absolute; top: -100px; left: -375px;" /> <h5 style=" font-weight: none; font-size: 18px; font-family: verdana; margin: 0px; margin-bottom: 20px; color: #96ff00">Login</h5> <form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post"> <label><input type="text" name="name" id="name" /><span style="color: #999999; font-size: 12px; font-family:verdana"> Name</span></label><br /> <label><input type="password" name="pass" id="pass" /><span style="color: #999999; font-size: 12px; font-family:verdana"> Password</span></label><br /> <input type="submit" id="submit" value="Login" style="margin-top: 10px;" /></form> </div> </body> The only problem is, there is no log out fuction. I see this script works with cookies and I gave it a try, and I ended up with nothing but errors. Does anyone one has an idea to how a logout script would look like for this script? Thanks in advance, Crow Link to comment https://forums.phpfreaks.com/topic/213361-a-cookie-problem/ Share on other sites More sharing options...
the182guy Posted September 14, 2010 Share Posted September 14, 2010 If your login system uses a cookie to check if the user is logged in then to logout all you need to do is delete that cookie. To delete a cookie just use setcookie() as if you were creating it but set the expiration date to in the past so that the browser will remove it. <?php // set the expiration date to one hour ago setcookie ("MyLoginPage", "", time() - 3600); ?> Link to comment https://forums.phpfreaks.com/topic/213361-a-cookie-problem/#findComment-1110890 Share on other sites More sharing options...
Cr0w1 Posted September 14, 2010 Author Share Posted September 14, 2010 hey the182guy, if I add <?php // set the expiration date to one hour ago setcookie ("MyLoginPage", "", time() - 3600); ?> on top of my code I get: Warning: Cannot modify header information - headers already sent by (output started at /home/zenzenl/public_html/projecten/index.php:6) in /home/zenzenl/public_html/projecten/index.php on line 34 Warning: Cannot modify header information - headers already sent by (output started at /home/zenzenl/public_html/projecten/index.php:6) in /home/zenzenl/public_html/projecten/index.php on line 35 Thanks, Crow Link to comment https://forums.phpfreaks.com/topic/213361-a-cookie-problem/#findComment-1110894 Share on other sites More sharing options...
the182guy Posted September 14, 2010 Share Posted September 14, 2010 That happens because there is some output or an echo before you try to delete the cookie. Cookies are part of a HTTP header and so they must be handled before any output occurs. Put all your cookie logic at the top of the script and seperate it from user interface messages and other HTML. Link to comment https://forums.phpfreaks.com/topic/213361-a-cookie-problem/#findComment-1110896 Share on other sites More sharing options...
macwise Posted September 14, 2010 Share Posted September 14, 2010 I could be wrong (green still here), but having just dealt with cookies, I think your problem is that your code is echoing something between your first setcookie block (to delete the cookie) and the second setcookie block. Try commenting out all of your echos and see if that fixes your problem? Link to comment https://forums.phpfreaks.com/topic/213361-a-cookie-problem/#findComment-1110897 Share on other sites More sharing options...
macwise Posted September 14, 2010 Share Posted September 14, 2010 Ahh, you beat me 182... Link to comment https://forums.phpfreaks.com/topic/213361-a-cookie-problem/#findComment-1110898 Share on other sites More sharing options...
Cr0w1 Posted September 14, 2010 Author Share Posted September 14, 2010 I dont completely understand what I have to do to edit it. My PHP knowledge is very minimum. I dont think I can throw this php stuff around without causing errors Thanks, Crow Link to comment https://forums.phpfreaks.com/topic/213361-a-cookie-problem/#findComment-1110900 Share on other sites More sharing options...
WatsonN Posted September 14, 2010 Share Posted September 14, 2010 What you could do it creat logout.php and put this in it Logout.php <?php setcookie("MyLoginPage", 0, time()-3600); header("Location: ./"); ?> or you could add this to your current file to be $url = (isset($_GET['p']) ? $_GET['p'] : 'home'); if ($url == "logout") { setcookie("MyLoginPage", 0, time()-3600); header("Location: ./"); } else { include 'projecten_.php'; $name = "0" ; } and have a link that is yourpage.php?p=logout -edit- fixed error in code -edit- Link to comment https://forums.phpfreaks.com/topic/213361-a-cookie-problem/#findComment-1111170 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.