To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 09-30-2003, 06:37 PM   #1
Nixon
He's so funny!
 
Join Date: Aug 2002
Posts: 247
Why do people code so complicatedly?

Hey all. I always seem to look at peoples code for simple things such as log-in scripts and I always think how ridiculously long they are. I don't understand why they need to be so long? Here is some ofmy login script. Are there any security issues or what?.......

PHP Code:

<?php

//Start session

session_start();

include(
"connectdb.inc.php");

// Member Login

$checkuserpass=mysql_query("select * from outwarinfo where username=\"$sessionusername\" and password=\"$sessionpassword\" ");
$found=mysql_num_rows($checkuserpass);


if(
$found == "1") { $loggedoutlink=*****; }

else

{

print
"<script language=\"JavaScript\">";
print
"location.href='login.php';";
print
"</script>";

}

session_register("sessionlogged");
$sessionlogged= "$loggedoutlink";

?>
Nixon is offline   Reply With Quote
Old 09-30-2003, 08:03 PM   #2
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
You are not doing any data scrubbing. What happens if the person enters ';SHOW TALBES; for their username?

This code would be hacked in about 30 seconds by anyone who targeted you.
drawmack is offline   Reply With Quote
Old 09-30-2003, 10:38 PM   #3
steadyguy
Senior Member
 
steadyguy's Avatar
 
Join Date: Jan 2003
Location: Brazil
Posts: 344
Many applications are much more complicated than your example. On the other hand, even your example could be simplified: remove the call to mysql_num_rows() and just test:
PHP Code:
if ($query = mysql_query($query))
It'll work because with a SELECT query, mysql_query() returns FALSE if no rows were found.

So one answer to your question is incomplete knowledge.

Another reason is varying experience levels: the more experienced you are, the more likely you are to know shortcuts, and thus write shorter code.

Yet another reason is because sometimes people find the long way more legible. This, of course, is somewhat relative, and depends not only on your experience but your personal coding style as well, not to mention background in other programming languages. While the somewhat PERLish
PHP Code:
$x = ($y != 0) ? 1/$y : $y/1;
is functionally the same as
PHP Code:
if ($y != 0)
    
$x = 1/$y;
else
    
$x = $y/1;
, not everyone is going to use the first way - despite the fact that it is effectively the same and is much more compact - and to some, perhaps it is more legible.

Another reason for complex code is because you can't plan explicitly for every possibility, so instead you plan for the one or two right/safe ones, and then call the rest errors. But you can't give people the standard error messages: not only do error messages turn people off, but they could also provide very useful information to an attacker.
steadyguy is offline   Reply With Quote
Old 10-01-2003, 11:01 AM   #4
ahundiak
Bright Member
 
ahundiak's Avatar
 
Join Date: Aug 2002
Posts: 1,608
Quote:
It'll work because with a SELECT query, mysql_query() returns FALSE if no rows were found.
From the manual
Quote:
Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of SQL statements, mysql_query() returns TRUE on success and FALSE on error. A non-FALSE return value means that the query was legal and could be executed by the server. It does not indicate anything about the number of rows affected or returned. It is perfectly possible for a query to succeed but affect no rows or return no rows.
http://www.php.net/manual/en/function.mysql-query.php

Also, adding ;SHOW TABLES won't cause a problem with mysql as multiple queries are not allowed in mysql_query. But I do agree that you should scrub your data.
ahundiak is offline   Reply With Quote
Old 10-01-2003, 06:10 PM   #5
Nixon
He's so funny!
 
Join Date: Aug 2002
Posts: 247
What do you mean by scubbing data?

Thanks.
Nixon is offline   Reply With Quote
Old 10-01-2003, 06:20 PM   #6
seby
Senior Member
 
Join Date: Nov 2002
Posts: 476
Quote:
Originally posted by Nixon
What do you mean by scubbing data?

Thanks.
sanitizing.. ie: making it fool proof for security reasons.

example, you have a url that is : page.php?id=1

and your query is like this:
SELECT * FROM table WHERE id = '".$_GET['id']."'

and someone does page.php?id=\1\

they will likely generate an error because of the " \ " in the url.
__________________
Counter-Strike Planet

Last edited by seby; 10-01-2003 at 06:27 PM.
seby is offline   Reply With Quote
Old 10-01-2003, 09:09 PM   #7
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
Also, the way you have it, people can use HTML code as their username. You might want to use htmlentities() on the username.
__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

The Three C's
Merve is offline   Reply With Quote
Old 10-02-2003, 10:01 AM   #8
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
In short people code copmlicatedly because it's safer to do so.
drawmack is offline   Reply With Quote
Old 10-06-2003, 10:24 AM   #9
steadyguy
Senior Member
 
steadyguy's Avatar
 
Join Date: Jan 2003
Location: Brazil
Posts: 344
ahundiak ... made my point pretty well didn't I? I'm so used to using an abstraction layer I wrote that does what I described, that I got myself confused ... thanks for setting me straight.
steadyguy is offline   Reply With Quote
Old 10-07-2003, 05:42 PM   #10
dustbuster
el pollo loco
 
Join Date: Nov 2002
Posts: 163
Quote:
Originally posted by Merve
Also, the way you have it, people can use HTML code as their username. You might want to use htmlentities() on the username.
I don't understand how a person putting HTML code as their username could be dangerous in that situation. Can you provide and example an what it would do? Thanks
dustbuster is offline   Reply With Quote
Old 10-07-2003, 05:51 PM   #11
stolzyboy
Super Moderator
 
stolzyboy's Avatar
 
Join Date: Sep 2002
Location: Fargo, North Dakota
Posts: 6,647
Quote:
Originally posted by dustbuster
I don't understand how a person putting HTML code as their username could be dangerous in that situation. Can you provide and example an what it would do? Thanks
look at the bottom of this forum, all the usernames...
well what if you let them use <img src=http://www.whateverdomain.com/nastypicture.jpg>

well then when you echo $username

instead of seeing: stolzyboy
you could see: nasty picture

catch my drift...

always sanitize your code...whether you think it may/may not hurt you in the end, better to be safe than sorry...
__________________

http://www.codesight.net - Custom Web Development, Web Hosting, Web Consulting
stolzyboy is offline   Reply With Quote
Old 10-07-2003, 06:05 PM   #12
dustbuster
el pollo loco
 
Join Date: Nov 2002
Posts: 163
Quote:
Originally posted by stolzyboy
look at the bottom of this forum, all the usernames...
well what if you let them use <img src=http://www.whateverdomain.com/nastypicture.jpg>

well then when you echo $username

instead of seeing: stolzyboy
you could see: nasty picture

catch my drift...

always sanitize your code...whether you think it may/may not hurt you in the end, better to be safe than sorry...
I realize that when setting up users one should sanitize the information. I was wondering why one would need to sanitize the information for the username in the code given at the beginning of this thread.
dustbuster is offline   Reply With Quote
Old 10-07-2003, 06:08 PM   #13
stolzyboy
Super Moderator
 
stolzyboy's Avatar
 
Join Date: Sep 2002
Location: Fargo, North Dakota
Posts: 6,647
Quote:
Originally posted by dustbuster
I realize that when setting up users one should sanitize the information. I was wondering why one would need to sanitize the information for the username in the code given at the beginning of this thread.
in THAT instance it may not matter depending, but things like <, ", ', etc... that are/can be in html tags may screw up your sql statement...
__________________

http://www.codesight.net - Custom Web Development, Web Hosting, Web Consulting
stolzyboy is offline   Reply With Quote
Old 10-07-2003, 06:18 PM   #14
dustbuster
el pollo loco
 
Join Date: Nov 2002
Posts: 163
Okay thanks
dustbuster is offline   Reply With Quote
Old 10-08-2003, 10:16 AM   #15
steadyguy
Senior Member
 
steadyguy's Avatar
 
Join Date: Jan 2003
Location: Brazil
Posts: 344
Probably a greater danger of HTML in usernames is the <script> tag ... with a line or two of JavaScript or VBScript an attacker could do all sorts of damage from trivial to downright dastardly.
steadyguy is offline   Reply With Quote
Reply

Bookmarks


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

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
Forum Jump


All times are GMT -4. The time now is 11:22 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.