Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 9th, 2003, 03:40 AM
Registered User
Join Date: Jul 2003
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default login and session

I still confused about session.
Can you help me to see sample code.
I wanna make login area for member. And after member log-in i wanna see their username on every page they visited.
I have already login.php (place for member inserting their id and password). And after login succeed, they will go to index2.php


Reply With Quote
  #2 (permalink)  
Old July 9th, 2003, 03:24 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

This is all fairly straight forward. Before ANY output is sent to the browser (including whitespace outside of the <?php and ?> tags), you must call session_start.

Here's a simple example:

-- login.php --

// attempting to log in after already logged in?
if(isset($_SESSION['logged_in']) && (true === $_SESSION['logged_in']))
    header('Location: index2.php');

// did user submit the login form?
   if(($_POST['username'] == 'nikolai') && // you have to figure out
      ($_POST['password'] == 'password'))  // how to verify logins
      // valid login, add username to session.
      $_SESSION['logged_in'] = true;
      $_SESSION['username'] = $_POST['username'];

      // redirect to index2.php
      header('Location: index2.php');
      echo "<b>Invalid login, please try again.</b>";
echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">\n"
   . "  Username: <input type=\"text\" name=\"username\"><br />\n"
   . "  Password: <input type=\"password\" name=\"password\"><br />\n"
   . "  <input type=\"submit\" name=\"submit\" value=\"Login\">\n"
   . "</form>\n";
--index2.php --

// verify logged in, if not, redirect to login page.
if(! isset($_SESSION['logged_in']))
   header('Location: login.php');

// if user is logging out, destroy session and redirect to login page.
if (isset($_POST['logout']))
   unset($_SESSION);   // delete session variable
   session_destroy();  // destroy data written to server filesystem
   header('Location: login.php');

// If we got here, session exists and user is NOT logging out.
// Count how many times the user has visited this page.

echo "Welcome, {$_SESSION['username']}!<br />\n";
echo "You've visited this page {$_SESSION['count']} times this session.<br />\n";

// logout button:
echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">\n"
   . "  <input type=\"submit\" name=\"logout\" value=\"Logout\">\n"
   . "</form>\n";
As always, it's a good idea to read through the manual. There's LOTS of documentation and sample code available.


Take care,

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pass a login name as session variable abhishek0807 ASP.NET 2.0 Professional 1 March 30th, 2007 12:49 AM
User Login - For Session helga@trisyn.co.za ADO.NET 2 September 25th, 2005 12:20 PM
session login with access db surethg Beginning PHP 3 November 19th, 2004 04:34 PM
How to write a cookie during a login session taoree ASP.NET 1.0 and 1.1 Basics 3 April 18th, 2004 02:40 PM
login page and session variables scdownload Classic ASP Basics 0 December 8th, 2003 06:13 PM

All times are GMT -4. The time now is 08:00 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.