Jump to content

please someone explain me this php script


varun7952

Recommended Posts

function quote_smart($value, $handle) {

 

  if (get_magic_quotes_gpc()) {

      $value = stripslashes($value);

  }

 

  if (!is_numeric($value)) {

      $value = "'" . mysql_real_escape_string($value, $handle) . "'";

  }

  return $value;

}

 

how this script works and this script not linked to any variable or any other place so how this scripts works without linked or connected to anywhere

(sorry if this ques is dumb but i m newbie in php

Link to comment
Share on other sites

This is just a function.

1. It checks if "magic quotes" are switched on. If NO, it add slashes to a $value.

2. If $value is not numeric function mysql_real_escape_string adds special characters.

 

BTW I just name you functions from this script. You can do it by yourself if go to http://www.php.net/manual/en/ an enter funciton names.

 

These actions are needed in order to prevent SQL injection.

Link to comment
Share on other sites

can any one tell me where is the problem in this script i gt the error

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\HW\test first login.php on line 44

 

and in username txt box whn i opened page its shows default value " i cant find where this " comes from please help me

 

 

<HTML>
<HEAD>
<TITLE>New Document</TITLE>
</HEAD>
<BODY>
<?php
$uname= "";
$pword= "";
$errormessage="";
  //==========================================
//	ESCAPE DANGEROUS SQL CHARACTERS
//==========================================

function quote_smart($value, $handle) {

   if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
   }

   if (!is_numeric($value)) {
       $value = "'" . mysql_real_escape_string($value, $handle) . "'";
   }
   return $value;
}

if($_SERVER['REQUEST_METHOD']=='POST'){
$uname =$_POST['user'];
$pword =$_POST['pass'];
$uname=htmlspecialchars($uname);
$pword=htmlspecialchars($pword);

// connection with database



$connection=mysql_connect("127.0.0.1","root","");
$db=mysql_select_db("addressbook",$connection);

if($db){
$uname=quote_smart($uname,$connection);
$upword=quote_smart($pword,$connection);
$SQL="SELECT * FROM address_data WHERE First_name=$uname AND Last_name=$pword";
$output=mysql_query($SQL);
$num_rows=mysql_num_rows($output);


if($output){
            if($num_rows=1){
            session_start();
            $_SESSION['login']="1";

            echo "logged in as".$uname;
             }
            else {
             ession_start();
             $_SESSION['login']="";
              echo $errormessage="invalid username or password";
             }
        }  else{
        echo $errormessage="invalid login";
}
}
           mysql_close($connection);
           }
?>

<FORM NAME ="form1" METHOD ="POST" ACTION ="">

Username: <INPUT TYPE = 'TEXT' Name ='user'  value="<?PHP echo $uname;?>" maxlength="20">
Password: <INPUT TYPE = 'TEXT' Name ='pass'  value="<?PHP echo $pword;?>" maxlength="16">

<P align = center>
<INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Login">
</P>

</FORM>

<P>
<?PHP print $errormessage;?>
</BODY>
</HTML>

Link to comment
Share on other sites

mysql_query will return false on error. False is a boolean value. Therefore if say

$output = mysql_query($sql);

SQL query fails and you don't check properly and just pass it to mysql_num_rows(); then it will yell about getting a boolean value (the value: false, because the query failed).

 

Perhaps the DB table "address_data" doesn't contain any data?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.