Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-25-2003, 08:25 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
Login With Sessions

There has been many inquiries/posts regarding login and sessions, I decided to create and test some scripts to share.

I've made a mock-up site consisting of 8 pages:
Code:
auth.php      // the meat and potatoes of this subject
connect.php // an excellent connection script
logout.php   // destroy session and re-direct to login

nav.php     // navigation include for site
index.php  // main page of site
link_1.php // page of site
link_2.php // page of site
link_3.php // page of site
Here is the SQL Schema for the table I Query:
Code:
CREATE TABLE users (
  user_id int(10) unsigned NOT NULL auto_increment,
  username varchar(20) NOT NULL default '',
  password varchar(20) NOT NULL default '',
  PRIMARY KEY  (user_id)
) TYPE=MyISAM;
if you copy and paste these files into your favorite text editor, edit the connect.php with your mysql settings, it should work on your server if sessions are supported.

auth.php
PHP Code:
<?
// Login & Session example by sde
// auth.php

// start session
session_start(); 

// convert username and password from _POST or _SESSION
if($_POST["username"])
{
  
$username=$_POST["username"];
  
$password=$_POST["password"];  
}
elseif(
$_SESSION["username"])
{
  
$username=$_SESSION["username"];
  
$password=$_SESSION["password"];
}

// start and register session variables
session_register("username");
session_register("password");

// connect to database
include("connect.php");

// query for a user/pass match
$result=mysql_query("select * from users 
  where username='" 
$username "' and password='" $password "'");

// retrieve number of rows resulted
$num=mysql_num_rows($result); 

// print login form and exit if failed.
if($num 1){
  
session_destroy();
  echo 
"You are not authenticated.  Please login.<br><br>
  
  <form method=POST action=index.php>
  username: <input type=text name=\"username\">
  password: <input type=password name=\"password\">
  <input type=submit>
  </form>"
;
  
  exit;
}
?>
connect.php
PHP Code:
<?
// Login & Session example by sde
// connect.php

// replace with your db info
$hostname="localhost";
$mysql_login="root";
$mysql_password="";
$database="test";

if (!(
$db mysql_connect("$hostname""$mysql_login" "$mysql_password")))
{
  print(
"Can't connect to mysql.");    
}
else
{
  if (!(
mysql_select_db("$database",$db)))
  {
    print(
"Can't connect to db.");
  }
}
?>
logout.php
PHP Code:
<?
// Login & Session example by sde
// logout.php

// you must start session before destroying it
session_start();
session_destroy();

echo 
"You have been successfully logged out.

<br><br>
You will now be returned to the login page.

<META HTTP-EQUIV=\"refresh\" content=\"2; URL=index.php\"> "
;
?>
nav.php
PHP Code:
<?
// Login & Session example by sde
// nav.php
?>

<a href=index.php>Home</a> |
<a href=link_1.php>link_1</a> | 
<a href=link_2.php>link_2</a> | 
<a href=link_3.php>link_3</a> |
<a href=logout.php>logout</a>

<br><br>
index.php
PHP Code:
<?
// Login & Session example by sde
// index.php

// include auth and nav
include("auth.php");

// begin content
include("nav.php");

echo 
"This is my home page.";
?>
link_1.php
PHP Code:
<?
// Login & Session example by sde
// link_1.php

// include auth and nav
include("auth.php");

// begin content
include("nav.php");

echo 
"This is my Link 1.";
?>
link_2.php
PHP Code:
<?
// Login & Session example by sde
// link_2.php

// include auth and nav
include("auth.php");

// begin content
include("nav.php");

echo 
"This is my Link 2.";
?>
link_3.php
PHP Code:
<?
// Login & Session example by sde
// link_3.php

// include auth and nav
include("auth.php");

// begin content
include("nav.php");

echo 
"This is my Link 3.";
?>
__________________
Mike
sde is online now   Reply With Quote
Old 08-16-2003, 03:20 PM   #2 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
i just used some of this code for a new site. it works perfectly.

thanks SDE.
__________________


Urban Clothing
Admin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
login session help please Namraw PHP 2 09-14-2004 03:26 PM
PHP code for Login Screen KEEBOD PHP 2 07-15-2004 05:18 PM
Sessions are constantly dropping mtlinfo PHP 3 03-26-2004 12:20 AM
php sessions a_o PHP 6 11-27-2003 02:05 AM
narrowed down login trouble trevor PHP 7 01-18-2003 05:50 PM


All times are GMT -8. The time now is 02:26 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting