Jump to content

Where to place code?


Trium918

Recommended Posts

Below I have two different logout scripts. The first

one is a logout script that only destroy $_SESSION.

The second one is a logout script that destroy $_SESSION

and $_COOKIE.

 

Problem: I am trying to write a script that will delect the cookie

and session using the first logout scipt format. Basically, I

want to combine the two using the first script format. I started

it in the Second script be it isn't working correctly.

 

Note: What session would I have to change in the first script?

I'll write it thanks!!

 

First Script Format

<?php
session_start();
$_SESSION['valid_user'];   // store  to test if they *were* logged i
// start output html

do_html_header("Logging Out");

if (!empty($_SESSION['valid_user']))
{
  if (isset($_SESSION['valid_user']))
  {
    unset($_SESSION['valid_user']); 
    session_destroy();
    echo "<p class='genmed'>Logged out.</p><br>";
    do_html_url("login.php", "Login");
  }
  else
  {
   // they were logged in and could not be logged out
    echo "Could not log you out.<br>";
  }
}
else
{
  // if they weren't logged in but came to this page somehow
  echo "You were not logged in, and so have not been logged out.<br>";
  do_html_url("login.php", "Login");
}
?>

 

Second Script Format

<?php
if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
   setcookie("cookname", "", time()-60*60*24*100, "/");
   setcookie("cookpass", "", time()-60*60*24*100, "/");
}

if(!$logged_in){
    echo "<p class='genmed'>You were not logged in, and so have not been logged out.</p><br>";
    do_html_url("login.php", "Login");;
}
else{
   /* Kill session variables */
   unset($_SESSION['valid_user']);
   unset($_SESSION['password']);
   $_SESSION = array(); // reset session array
   session_destroy();   // destroy session.

   echo "<p class='genmed'>Logged out.</p><br>";
   do_html_url("login.php", "Login");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/49544-where-to-place-code/
Share on other sites

Ok, I got this working, but is there a better way?

Yes or NO!

 

Note: I am not asking for anyone to write!

 

 

<?php
if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass'])){
   setcookie("cookname", "", time()-60*60*24*100, "/");
   setcookie("cookpass", "", time()-60*60*24*100, "/");
}

if (!empty($_SESSION['valid_user']) && !empty($_SESSION['password']))
{
  if (isset($_SESSION['valid_user']) && isset($_SESSION['password']))
  {
    unset($_SESSION['valid_user']); 
    unset($_SESSION['password']);
    $_SESSION = array(); // reset session array
    session_destroy();   // destroy session.
    echo "<p class='genmed'>Logged out.</p><br>";
    do_html_url("login.php", "Login");
  }
  else
  {
   // they were logged in and could not be logged out
    echo "Could not log you out.<br>";
  }
}
else
{
  // if they weren't logged in but came to this page somehow 
  if(!$logged_in){
    echo "<p class='genmed'>You were not logged in, and so have not been logged out.</p><br>";
    do_html_url("login.php", "Login");;
  }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/49544-where-to-place-code/#findComment-242895
Share on other sites

Logout is a function that I create to show

the user login or prompt the user to login.

 

Sets the value of the logged_in variable, which can be used in your code

$logged_in = checkLogin();

 

I am just trying to write the logout script. The other function should be

a factor, but if they are just ask. I will provide the information that you

need in order to assist me. Thanks.

 

 

You don't define $logged_in anywhere. Also, if that do_html_url function is redirecting somewhere, theres not much point echoing anything prior to calling it.

 

<?php
/**
* Determines whether or not to display the login
* form or to show the user that he is logged in
* based on if the session variables are set.
*/
function displayLogin(){
   global $logged_in;
   if($logged_in){
      echo "<h1>Logged In!</h1>";
      echo "Welcome <b>$_SESSION[valid_user]</b>, you are logged in. <a href=\"logout.php\">Logout</a>";
   }
   else{
?>

<h1>Login</h1>
<form action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember">
<font size="2">Remember me next time</td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
<tr><td colspan="2" align="left"><a href="register.php">Join</a></td></tr>
</table>
</form>

<?
   }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/49544-where-to-place-code/#findComment-243005
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.