Jump to content

Sessions Logout


jandrews3

Recommended Posts

I'm a high school Spanish teacher and have designed a website for students to login with session protected security to their own profiles so I can track their work. However, I need to ensure a student logoff ability beyond that of just quitting the web browser. Below is the code I have put at the top of all "secured" pages. Is there a way I can put a link (a logout button) which will delete the $_SERVER['PHP_AUTH_PW'] variable and would therefore require any other users who sit down to that computer to re-enter a password (i.e. - their own)? Thank you!

session_start();
$link = mysql_connect("ositowebsolutions.com","ositoweb","******") or die("Could not connect: ".mysql_error());
        mysql_select_db("my_database")or die("Could not select database: ".mysql_error());

  $query = "SELECT * FROM chs_students WHERE uname = '{$_SERVER['PHP_AUTH_USER']}' and pword = '{$_SERVER['PHP_AUTH_PW']}'";
  $result = mysql_query($query) or die("Could not perform query: ".mysql_error());
$row = mysql_fetch_array($result);
$uname = $row['uname'];
$pword = $row['pword'];
$id = $row['id'];


if (!isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])
    || !($_SERVER['PHP_AUTH_USER'] == $uname && $_SERVER['PHP_AUTH_PW'] == $pword))
{
    header('WWW-Authenticate: Basic realm="Authorization Required!"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Authorization Required!';
    exit;
}

 

 

Link to comment
Share on other sites

I'm sorry. I'm perpetually a beginner. I created a link that returned the user to the home page with the variable logout=true. Then on the home page, I added ...

<?
if ($logout = "true"){
session_start();

unset($_SERVER['PHP_AUTH_USER']);
unset($_SERVER['PHP_AUTH_PW']);
}

because those are the two session variables used. However, it didn't work for me. I realize I'm probably not using the right variables there. Given the posting I made earlier, can you help with the unset variables? Thank you.

Link to comment
Share on other sites

Thank you but I'm afraid that example doesn't quite fit what I'm trying to do. After the student "logs out" I want the browser to go back to the home page which does NOT require a username or password to view (think of it like a bank's homepage - you only need to password to see the sensitive stuff).

 

What I'm trying to do is to send the browser back to the homepage and clear out the session variables so that the next time a student clicks the "login" button, it will act as if there is no session in place. I don't know. This may not be possible. The coding that SMERNY gave me looks promising but I'm messing up the variables somehow. Can anyone help me? Thank you.

Link to comment
Share on other sites

It's because you aren't actually using session variables... I know what you're doing is a different way of doing it, but I am not exactly familiar with it... I would have used session variables... like

 

$_SESSION['user'] = $user;

$_SESSION['pw'] = $pw;

 

and then you could unset them with

 

unset($_SESSION['user']);

unset($_SESSION['pw']);

 

but again, I'm unfamiliar with the way you are doing it.

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.