Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 9th, 2003, 03:40 AM
Registered User
 
Join Date: Jul 2003
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

Thanx

 
Old July 9th, 2003, 03:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 --
Code:
<?php
session_start();

// 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(isset($_POST['username']))
{
   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');
   }
   else
   {
      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 --
Code:
<?php
session_start();

// 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.
++$_SESSION['count']

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.

  http://www.php.net/session


Take care,

Nik
http://www.bigaction.org/





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





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