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 01-19-2003, 04:26 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
Example Login/Session Techniques

Since 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.

Due to a limit in characters per post, I will post the source for each page in its own post.
sde is offline   Reply With Quote
Old 01-19-2003, 04:28 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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;
}
?>
sde is offline   Reply With Quote
Old 01-19-2003, 04:29 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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.");
  }
}
?>
sde is offline   Reply With Quote
Old 01-19-2003, 04:30 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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\"> "
;
?>
sde is offline   Reply With Quote
Old 01-19-2003, 04:30 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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>
sde is offline   Reply With Quote
Old 01-19-2003, 04:33 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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.";
?>
sde is offline   Reply With Quote
Old 01-19-2003, 04:33 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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.";
?>
sde is offline   Reply With Quote
Old 01-19-2003, 04:34 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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.";
?>
sde is offline   Reply With Quote
Old 01-19-2003, 04:35 PM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
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.";
?>
sde is offline   Reply With Quote
Old 01-19-2003, 04:57 PM   #10 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 285
technobard is on a distinguished road
Nicely done. It looks like the stuff that NUMs are made of.
technobard is offline   Reply With Quote
Old 01-19-2003, 05:00 PM   #11 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
=) good thinking .. i didn't feel like doin a whole lot of explaining though. i'll port it over to a num soon.
sde is offline   Reply With Quote
Old 01-19-2003, 05:25 PM   #12 (permalink)
trevor
Code Monkey
 
Join Date: Jan 2003
Location: Canada
Posts: 91
trevor is on a distinguished road
thanks man,
trevor is offline   Reply With Quote
Old 01-19-2003, 05:40 PM   #13 (permalink)
Ilya020
Techno Rat
 
Ilya020's Avatar
 
Join Date: Jan 2003
Location: San Diego
Posts: 559
Ilya020 is on a distinguished road
Send a message via AIM to Ilya020
I think its a giant spam message.

but really, nicely done..hopefully..I will need this soon!


Ilya
__________________
> SELECT * FROM users WHERE clue > 0
0 rows returned
Ilya020 is offline   Reply With Quote
Old 01-19-2003, 06:17 PM   #14 (permalink)
trevor
Code Monkey
 
Join Date: Jan 2003
Location: Canada
Posts: 91
trevor is on a distinguished road
sde,

I have a few questions. If your not to busy can you come to the codenewbie irc?

thanks
trevor 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 07:14 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