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 08-05-2007, 07:26 AM   #1 (permalink)
Fascination
mv life.conf /dev/null/
 
Fascination's Avatar
 
Join Date: Aug 2007
Location: Jersey, Channel Islands, UK
Posts: 4
Fascination is on a distinguished road
PHP Sanitisation on User Input

Hi there.

I’ve just finished following sde’s tutorial on session authentication and have got it working successfully (after lowering the error reporting level to remove the issues where it gets upset about the undeclared ‘username’ and ‘password’).
However, I was wondering where exactly I would initiate some form of sanitisation to prevent users from injecting unwanted code into the fields (and of course prevent the resulting chaos afterwards).
My aim is to use LOWER for the username (as the username will always be characters only, lowercase) and to allow numeric, upper and lower case characters for the password. My issue is that Im not quite sure where I should be inserting the functions to verify the user input:

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

// start session 
session_start();  

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

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

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

// print login form and exit if failed. 
if($num < 1){ 
  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; 
} 
?>
Any help you could provide on this would be greatly appreciated, thank you.

- Fasc
Fascination is offline   Reply With Quote
Old 08-05-2007, 07:41 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 705
DJMaze is on a distinguished road
Always sanitize at first level (input).
This means that you must verify each $_POST, $_GET, $_COOKIE, $_SERVER and $_FILES entry first BEFORE you put them somewhere else.
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 08-05-2007, 08:02 AM   #3 (permalink)
Fascination
mv life.conf /dev/null/
 
Fascination's Avatar
 
Join Date: Aug 2007
Location: Jersey, Channel Islands, UK
Posts: 4
Fascination is on a distinguished road
Quote:
Originally Posted by DJMaze View Post
Always sanitize at first level (input).
This means that you must verify each $_POST, $_GET, $_COOKIE, $_SERVER and $_FILES entry first BEFORE you put them somewhere else.
So, would it be logical to code:

Code:
if($_POST){ 
  $_SESSION['username']=$_POST FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH["username"]; 
  $_SESSION['password']=$_POST FILTER_SANITIZE_STRING["password"];   
}
Fascination is offline   Reply With Quote
Old 08-05-2007, 10:42 AM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 705
DJMaze is on a distinguished road
no:
PHP Code:
if (!empty($_POST['username']) && !empty($_POST['password']))
{
    
// magic quotes suck hard
    
$_SESSION['username'] = (get_magic_quotes_gpc() ? stripslashes($_POST['username']) : $_POST['username']);
    
$_SESSION['password'] = (get_magic_quotes_gpc() ? stripslashes($_POST['password']) : $_POST['password']);

Then at the database level ("input" to database)
PHP Code:
$result=mysql_query("select * from users  
  where username='" 
mysql_real_escape_string($_SESSION['username']) . "' and password='" mysql_real_escape_string($_SESSION['password']) . "'"); 
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 08-05-2007, 01:39 PM   #5 (permalink)
Fascination
mv life.conf /dev/null/
 
Fascination's Avatar
 
Join Date: Aug 2007
Location: Jersey, Channel Islands, UK
Posts: 4
Fascination is on a distinguished road
Sorry, I dont mean to be stupid but I am curious (and thank you so far for your help). Why the need though to establish 'get_magic_quotes_gpc' - I thought that was on by default? Or is it simply needed to make use of the stripslashes afterwards?
Fascination is offline   Reply With Quote
Old 08-05-2007, 01:57 PM   #6 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 705
DJMaze is on a distinguished road
Quote:
Originally Posted by Fascination View Post
'get_magic_quotes_gpc' - I thought that was on by default?
That's the whole crap setting in PHP 4. It should be off
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 08-06-2007, 08:36 AM   #7 (permalink)
Fascination
mv life.conf /dev/null/
 
Fascination's Avatar
 
Join Date: Aug 2007
Location: Jersey, Channel Islands, UK
Posts: 4
Fascination is on a distinguished road
Ah I see.

Although magic quotes will suffice for now, judging by your comment tag you dont think its ideal; what method do you think I should consider studying to improve upon it?

Many thanks for your help so far.
Fascination 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using regular expressions to clean HTML in user input NirTivAal PHP 3 05-23-2006 10:19 PM
need help with copying backwards rogue Standard C, C++ 9 04-24-2005 04:39 PM
User input teknomage1 Standard C, C++ 8 04-22-2005 12:28 AM
PHP 5.0.4 and 4.3.11 Released sde Code Newbie News 0 04-20-2005 10:56 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM


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