[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 08-07-2005, 12:39 PM   #1 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future

Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
Thumbs up A little better login script (with MySQL) - and page protection with session controls

Hi,

Actually, I hope this isn't in the wrong forum, since it's been done before I'm sure, but just a little enhancement to SecondVersion's post a bit downward...

Now, I actually send an HTTP re-direct in case a certain nav panel on the side or top of the page needs to be updated upon a login. Because sessions and cookie variables don't take effect or are recognized until the page AFTER login, an HTTP redirect is used to keep things current without having to check filenames This is entirely optional but I think it works better and I recommend it.

Note: For the re-direct, PHP function header() can be used:

PHP Code:
header ('Location: page.php');
before any other headers are sent and that will redirect before the page displays any content (if there IS any to be displayed) - the page won't even be sent to the browser (which is WHY headers have to go before stuff sent to the browser) until that header tag to redirect it.

This SHOULD really work (haven't tried it) but I scanned the code for errors. Please note! This is NOT a very secure script! (I've written better...)

index.html (login page)
HTML Code:
<html>
<head>
<title>Login</title>
</head>
<body>

<form method="post" action="login.php">
Username: <input type="text" name="username">
<BR><BR>
Password: <input type="password" name="password">
<BR><BR>
<input type="submit" value="Login">
</form>

</body>
</html> 
Now, when the user presses the "Login" button, it goes to the action:

login.php
PHP Code:
<?php

//Start up the session for when we set the session variables!

session_start();

/*
Assign these a value - I'm pretty sure this prevents the variables being sent via GET in the URL.

I'm also gonna shorten the variable names to make them easier to work with.
*/

$uname = $_POST['username'];
$pwd = $_POST['password'];

//Now, check to see if the user filled out both fields.

if (empty($uname) || empty($pwd))
{
    echo
'You must fill out both the username and password.';
    exit;
}

/*
Now, encrypt the $pwd variable (assuming and HOPEFULLY) since you encrypted them in the MySQL database. I prefer md5. It's ALWAYS 32 characters long.
*/

$pwd = md5($pwd);

//connect...
mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('db_name');

//Run the query...
$query = "SELECT * FROM users WHERE username='$uname' AND password='$pwd' LIMIT 1";

$result = mysql_query($query);

//How many results...?
$num = mysql_num_rows($result);


//No results... meaning they didn't match...
if ($num < 0)
{
    echo
'That is an invalid username/password combo. Please try again.';
    exit;
}

//now... if they DID match, set the session variables and display a successful login.

//For this example, I'm just storing username - but in more advanced sites,
//you will want to store more and check them every protected page
//to make sure they are valid and it will be more secure.

if ($num == 1)
{
    
$_SESSION['username'] = $username;

//Done with the PHP! Display the success and redirect.
?>
<html>
<head>
<meta http-equiv="REFRESH" content="0; url=protected_page.php">
<title>Login successful!</title>
</head>
<body>
Login successful... please wait until you are redirected...
</body>
</html>
<?php
//Close the IF block
}
?>
Now, this script will store the username in a session variable and redirect to "protected_page.php".

The script for that is as follows:

protected_page.php
PHP Code:
<?php

//Start the session! Always do this FIRST when working with sessions!
session_start();

//Does the session variable we set earlier exist?
if (!isset($_SESSION['username']))
{
     echo
'You have to be logged in to see this page!';
     exit;
}

//Now, if the script came this far, the user is authenticated and can see the page.

?>
<html>
<head>
<title>Secret Page</title>
</head>
<body>
<p>Here is the secret page!</p>

<p>I bet you are glad you get to see this secret page.</p>

<p>Woopdie-doo...</p>
</body>
</html>
Hope it works... lol... it should

EDIT! Guess I should tell ya how to log out.

It's really easy!

logout.php
PHP Code:
<?php

//Start the session, of course. Actually maybe it's ironic,
//since what we want to do is logout. But just start the
//session for now. lol...
session_start();

//Destroy the session variable we set earlier.

unset($_SESSION['username']);

//Destroy the session entirely now.

session_destroy();

//Done!

?>
<html>
<head>
<title>Logged out!</title>
</head>
<body>
You have been logged out. Session destroyed.
</body>
</html>
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!

Last edited by compuXP; 08-08-2005 at 06:19 AM. Reason: right forum? oops...
mholt is offline  
Old 08-07-2005, 12:43 PM   #2 (permalink)
NPQ's PA, Slave, and On Call Coder

Technical Services


 
Eric's Avatar
 
Join Date: Mar 2005
Posts: 4,546
0.71 NP$ (Donate)

Eric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond repute

Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse
__________________
Eric is offline  
Old 08-07-2005, 01:21 PM   #3 (permalink)
Pro Coder & Designer
 
xlusive's Avatar
 
Join Date: Apr 2005
Location: Netherlands
Posts: 964
101.50 NP$ (Donate)

xlusive is just really nicexlusive is just really nicexlusive is just really nicexlusive is just really nicexlusive is just really nice


if you didnt know session_start(); and then space and comments above it, isnt allowed. it will give you header problems.
__________________
Online Dragonball Game
xlusive is offline  
Old 08-07-2005, 01:24 PM   #4 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future

Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
Actually, it just has to be sent before any other headers It works fine
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 08-07-2005, 02:29 PM   #5 (permalink)
Senior Member
 
Join Date: May 2003
Posts: 2,211
6,170.25 NP$ (Donate)

adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough

Breast Cancer
Quote:
Originally Posted by xlusive
if you didnt know session_start(); and then space and comments above it, isnt allowed. it will give you header problems.
you are allowed to put anything before session_start(); as long as it doesnt get passed to the browser for rendering. only then you will get header problems.
adam_uk is offline  
Old 08-07-2005, 11:22 PM   #6 (permalink)
NamePros Member
 
Join Date: Jan 2005
Location: Texas USA
Posts: 71
203.00 NP$ (Donate)

Outer is an unknown quantity at this point


You might want to add re-authentication and IP checking in the script also. Added security and the IP check helps with preventing session Hijacking...
__________________
I wonder...
Outer is offline  
Old 08-08-2005, 01:45 AM   #7 (permalink)
.PJ
NamePros Regular
 
.PJ's Avatar
 
Join Date: Aug 2004
Posts: 369
0.50 NP$ (Donate)

.PJ is on a distinguished road


Good job, if I didn't write my own code and still stole others, I would probs steal it :P

PJ
__________________
L33TSig.net
.PJ is offline  
Old 08-08-2005, 06:11 AM   #8 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future

Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
No no - use it if you want! Share it with the WORLD... that took me 5 mins to write anyway...

I updated the script and added a logout function!
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Old 08-08-2005, 10:11 AM   #9 (permalink)
Senior Member
 
Porte's Avatar
 
Join Date: May 2005
Location: Somewhere on earth!
Posts: 3,528
21.30 NP$ (Donate)

Porte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud ofPorte has much to be proud of


Cool and standard. Good work.
__________________
Custom WordPress theme design. Top notch free WordPress themes
Custom Theme Design
Porte is offline  
Old 08-08-2005, 12:00 PM   #10 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future

Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
NEW!

http://www.namepros.com/code/113786-fullyfunctional-member-system-login-logout-admin.html

If you're looking for something better - read that!
__________________
codeboards

A high-quality community of programmers -- Join today and post! We want new members!
mholt is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP / MySQL Login Problem will7 Programming 10 04-11-2005 01:40 PM
60.000 Templates, scripts, fonts, banners etc. $9.95 atkims Web Development Wanted 19 11-16-2004 09:48 AM
Tutorial: Getting Started With MySQL (The Basics) deadserious Webmaster Tutorials 3 04-18-2004 01:17 PM

Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 12:08 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85