WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1]
SELECT rating_username, rating_rating, rating_ip FROM wp_ratings WHERE rating_postid =

Setting up the logout functions to clear the user session ID

Vote This Post DownVote This Post Up (+4 rating, 4 votes)
Loading ... Loading ...

PHP & MySQL Login Tutorial Series
Pt1: Introduction
Pt2: Setting up the database
Pt3: The front end and user validation
Pt4: The code to privatize a page
Pt5: Logout function and clearing the session ID

Part 5: Logout functions

Our final function is the logout function that will wipe our session. That function is simpler and looks like this:

<?php
function logout() {
$sessionid =$_COOKIE[test_account];
@setcookie (”test_account,”,time()-99999, ‘/’, ”);
mysql_query(”DELETE FROM user_sessions WHERE sessionid=’$sessionid’”);
}
?>

We first grab any session id out of the user’s submitted cookie. We then delete the cookie on the user’s computer by setting the cookie expiration date to sometime in the past (effectively deleting it), and we finally delete the session record from our database.

Before we can finish off the program we have two more tasks. The first is to throw an error message if the user enters invalid login credentials, and the second is to print out a greeting to the user if they’re logged in. Replace the HTML code from before with the following:

<?php
if($userid > 0) { echo “Welcome to our site, user #$userid (<a href=’?logout’>Click here to logout</a>)”; } else {
if($login_status != ” && $login_status == 0) { echo “Invalid username/password combo.<br>”; }
?>
<form action=”sample.php” method=”POST”>
<input type=text name=username>
<input type=password name=password>
<input type=submit value=”Log In”>
</form>
<?php } ?>

We first to check to see if our status function call returned a user id. If it did we can print out a welcome to the user, giving them his or her user id. We also provide a link for logging out; simply call the same page with “logout” specified as a URL GET variable to log out. If we don’t have a user id, we’ll print out the login form. We also check the login_status variable to see if there was an error. If there was we’ll print this out to the user.

This tutorial covers the basic concepts of a user login system, however it certainly doesn’t cover everything. There are many websites on the internet that can assist you with the specifics of setting up a database table, basic programming concepts, and anything in between. The full sample program can be found here. Note: In our example we assume you already have a connection to the database established, however sample.php includes the basic MySQL database connection functions.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Webnews
  • MisterWong
  • Y!GG

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>