Jump to content

A cookie problem


Cr0w1

Recommended Posts

Hello :D

 

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.