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 11-03-2003, 08:35 AM   #1
Chrysanthus
Php freak
 
Chrysanthus's Avatar
 
Join Date: Mar 2003
Location: Sweden
Posts: 413
Help with makeing the code beter, faster, and find stupid code lops!!

Hi

it woulde be great if some one coulde take a look att the code and se if i can make some improvments and if there are any botnecks i the code!!

<?php
$username=$_POST["username"];
$password=$_POST["password"];

include ("../managment/mysqlcondbrr.php");


$mysqlcon = "select * from reg_info where username = '$username' and password = '$password';";
$result = mysql_query($mysqlcon) or die("Query failed");

if (mysql_num_rows($result) == '1') {

print "<p><center><big><big>Welcome $username Please click <a href=\"isloggedin.php\">here to continue to flameline.com</big></big></center></p>";

$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
$_SESSION["validuser"] = "1";
$_SESSION["loggins"] = "1";
}
else
{
$_SESSION["unvaliduser"] = "0";
print "<p><center><big><big>Sorry, incorrect Password $password. or Nickname $username.</big></big></center></p>";
print "<p><center><big>if not a member <a href=\"register.php\">click here to register</big></center></p>";
}

?>
__________________
Let the music play, it will be your getway..

The music is the way....



Chrysanthus is offline   Reply With Quote
Old 11-03-2003, 09:05 AM   #2
HalfaBee
Not very lazy.
 
Join Date: Jun 2003
Location: Sydney, Australia
Posts: 1,851
When you post php use the php button just above or put the code in [ PHP] [/ PHP] tags ( without the space.
Like this.
PHP Code:
<?php
$username
= $_POST["username"];
$password = $_POST["password"];

include (
"../managment/mysqlcondbrr.php");

$mysqlcon = "select * from reg_info where username = '$username' and password = '$password';";
$result = mysql_query($mysqlcon) or die("Query failed");

if (
mysql_num_rows($result) == '1') {
    print
"<p><center><big><big>Welcome $username Please click <a href=\"isloggedin.php\">here to continue to flameline.com</big></big></center></p>";

    
$_SESSION["username"] = $username;
    
$_SESSION["password"] = $password;
    
$_SESSION["validuser"] = "1";
    
$_SESSION["loggins"] = "1";
} else {
    
$_SESSION["unvaliduser"] = "0";
    print
"<p><center><big><big>Sorry, incorrect Password $password. or Nickname $username.</big></big></center></p>";
    print
"<p><center><big>if not a member <a href=\"register.php\">click here to register</big></center></p>";
}

?>
Your code has no loops, unless they are in the include file.

HalfaBee
__________________
The lazy man always finds the best way!
Q: Who invented the auto-pilot?
A: The lazy pilot!
HalfaBee is offline   Reply With Quote
Old 11-03-2003, 09:49 AM   #3
shareaweb
Member
 
shareaweb's Avatar
 
Join Date: Apr 2003
Location: Philippines
Posts: 59
optimization

include_once() is marginally faster. Any reason why your selecting all instead of just the columns you need? Just a thought...
Any do you have output bufferring enabled?
__________________
Practice Sesquipedilianism (and turn Reg Globals OFF)...
shareaweb is offline   Reply With Quote
Old 11-03-2003, 10:41 AM   #4
Chrysanthus
Php freak
 
Chrysanthus's Avatar
 
Join Date: Mar 2003
Location: Sweden
Posts: 413
Your code has no loops, unless they are in the include file.

HalfaBee

Nop that's only the db connection !!

///

shareaweb


include_once() is marginally faster. Any reason why your selecting all instead of just the columns you need? Just a thought...

Answer: That's a good question i shoulde use only password and username columns??



Any do you have output bufferring enabled?

Whats tha't what do that Improve
__________________
Let the music play, it will be your getway..

The music is the way....



Chrysanthus is offline   Reply With Quote
Old 11-03-2003, 02:32 PM   #5
BuzzLY
2($infinity) && $beyond
 
BuzzLY's Avatar
 
Join Date: Nov 2002
Location: Star Command
Posts: 2,535
Re: Help with makeing the code beter, faster, and find stupid code lops!!

Quote:
Originally posted by Chrysanthus
$_SESSION["loggins"] = "1";
PHP Code:
session_destroy();
if (
$_SESSION['loggins'] != "1") {
    echo
"Oh my God, they killed Kenny!!!";
}


BTW... use single quotes in your $_SESSION array, not double quotes.
__________________
New to the board? Check out the guidelines
| Color Picker | Blogification |
¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
With all its sham, drudgery, and broken dreams, it's still a beautiful world.
BuzzLY is offline   Reply With Quote
Old 11-03-2003, 03:48 PM   #6
Chrysanthus
Php freak
 
Chrysanthus's Avatar
 
Join Date: Mar 2003
Location: Sweden
Posts: 413
okay but i have one stupid question that i wanna ask what makes the diffrence with use single quotes insted of dubbel ???


PHP:
--------------------------------------------------------------------------------

session_destroy();
if ($_SESSION['loggins'] != "1") {
echo "Oh my God, they killed Kenny!!!";
}
__________________
Let the music play, it will be your getway..

The music is the way....



Chrysanthus is offline   Reply With Quote
Old 11-03-2003, 05:09 PM   #7
goldbug
50,000 Watts of Goodwill
 
goldbug's Avatar
 
Join Date: May 2003
Location: Rummaging through your garbage.
Posts: 1,326
IIRC, one reason is because PHP evaluates everything in double quotes (which is why you can throw variable names in there), and just treats single-quoted text as a scalar value, throwing it around as-is. Single quotes thusly provides you with a very minor performance increase.

my 2c.
__________________
Many eyes make few mistakes

goldendance
goldbug is offline   Reply With Quote
Old 11-03-2003, 05:14 PM   #8
goldbug
50,000 Watts of Goodwill
 
goldbug's Avatar
 
Join Date: May 2003
Location: Rummaging through your garbage.
Posts: 1,326
Also, you're setting a bunch of session variables to 0 or 1.

Is this supposed to indicate a true/false state of some kind? (Are numbers really required?)

If possible consider something like:

PHP Code:
$_SESSION['foo'] = TRUE;
then it makes your code a little cleaner:
PHP Code:
if ($_SESSION['foo']) {
...
}
Of course, you could still use the if statement above with your current values (0/1 evaluate to false/true).
__________________
Many eyes make few mistakes

goldendance
goldbug is offline   Reply With Quote
Old 11-03-2003, 05:36 PM   #9
HalfaBee
Not very lazy.
 
Join Date: Jun 2003
Location: Sydney, Australia
Posts: 1,851
TRUE & FALSE are just numbers.

It could take longer to put a defined number rather than an explicit number.

But I prefer TRUE/FALSE also.


Also I would not use
$_SESSION["unvaliduser"] = "0";

If they are not valid set
$_SESSION["validuser"] = 0;

Halfabee
__________________
The lazy man always finds the best way!
Q: Who invented the auto-pilot?
A: The lazy pilot!
HalfaBee is offline   Reply With Quote
Old 11-03-2003, 06:00 PM   #10
Chrysanthus
Php freak
 
Chrysanthus's Avatar
 
Join Date: Mar 2003
Location: Sweden
Posts: 413
okay sow this is the best way! my ide was that give the user a login level per specified user but that can be a later brainstorm.


<?php
$username = $_POST["username"];
$password = $_POST["password"];

include ("../managment/mysqlcondbrr.php");

$mysqlcon = "select * from reg_info where username = '$username' and password = '$password';";
$result = mysql_query($mysqlcon) or die("Query failed");

if (mysql_num_rows($result) == '1') {
print "<p><center><big><big>Welcome $username Please click <a href=\"isloggedin.php\">here to continue to flameline.com</big></big></center></p>";

$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
$_SESSION['validuser'] = TRUE;
$_SESSION["loggins"] = "1";
} else {
$_SESSION['unvaliduser'] = FALSE;
print "<p><center><big><big>Sorry, incorrect Password $password. or Nickname $username.</big></big></center></p>";
print "<p><center><big>if not a member <a href=\"register.php\">click here to register</big></center></p>";
}

?>
BuzzLY

did you mean thate code shoulde look like this

$_SESSION['loggins'] = "1";
__________________
Let the music play, it will be your getway..

The music is the way....



Chrysanthus is offline   Reply With Quote
Old 11-03-2003, 06:07 PM   #11
HalfaBee
Not very lazy.
 
Join Date: Jun 2003
Location: Sydney, Australia
Posts: 1,851
There is no need for the 'unvaliduser' session variable.
Just set $_SESSION['validuser']=FALSE;

HalfaBee
__________________
The lazy man always finds the best way!
Q: Who invented the auto-pilot?
A: The lazy pilot!
HalfaBee is offline   Reply With Quote
Old 11-03-2003, 06:10 PM   #12
Chrysanthus
Php freak
 
Chrysanthus's Avatar
 
Join Date: Mar 2003
Location: Sweden
Posts: 413
Thats rigtht way do i need that if there are now value in the session file tha's not TRUE The user will be kicked!
__________________
Let the music play, it will be your getway..

The music is the way....



Chrysanthus is offline   Reply With Quote
Old 11-03-2003, 06:15 PM   #13
Chrysanthus
Php freak
 
Chrysanthus's Avatar
 
Join Date: Mar 2003
Location: Sweden
Posts: 413
ahha PHP button thanks i wounder how all you user gott color now i know that to!
__________________
Let the music play, it will be your getway..

The music is the way....



Chrysanthus is offline   Reply With Quote
Old 10-26-2006, 05:51 PM   #14
rythmic
Junior Member
 
Join Date: Oct 2006
Posts: 1
Hi!

I think that the logincheck that you use is not safe. It can easily be passed using sql injection where you would just limit your answer to return one line.

You should check the password so that it actually equals the password in the database.. like this example below:

PHP Code:
else{
    
/* Let's validate login */

    
$given_user = $_POST['username'];
    
$given_pass = md5($_POST['password']);
    
    
$query = "SELECT $tbl_users_uid,$tbl_users_pass,$tbl_users_isb FROM $tbl_users WHERE $tbl_users_uname='$given_user' LIMIT 1";
    
$result = mysql_query($query,$connection);
    if(
mysql_num_rows($result) != 0){
        
$row = mysql_fetch_assoc($result)or die(mysql_error());
        
        
/* If password is correct and user is not blocked login should be approved */
        
if($row["$tbl_users_pass"] == $given_pass && $row["$tbl_users_isb"]!= 1){
        
$_SESSION['user_name'] = $given_user;
        
$_SESSION['user_id'] = $row["$tbl_users_uid"];
        
register_login($row["$tbl_users_uid"]);
        unset(
$_SESSION['failed_attempts']);
        
header('Location: index_inside.php');
    }
It is taken from a login code from one of my pages and as you can see there is little hope of getting through without providing a correct password for an existing user.

cheers
rythmic 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 03:37 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.