Jump to content

When to add slashes?


Jaguar

Recommended Posts

I'm confused on when I should add slashes to post and get data. I guess I don't understand compeltely how php works. Do I have to add slashes right away or do I only need to add slashes if I'm inserting into a database? Assuming magic quotes are off, is this ok?

 

if( $_POST['submit'] == "submit" )
{	
$name = $_POST['name'];
$pass = $_POST['pass'];

$pass = mysql_real_escape_string( $pass );
$name = mysql_real_escape_string( $name );

$result = mysql_query( "SELECT Name, Pass FROM Users WHERE Name = '$name' AND Pass = '$pass' LIMIT 1" );
}

 

Also what if I was just printing or using post/get data in PHP, not using a database. Is there any danger?

 

echo $_POST['name'];

$name = $_POST['name'];
$pass = $_POST['pass'];

$namepass = $name . ':' . $pass;

echo $namepass;

Link to comment
Share on other sites

I was worried that they could insert PHP code where I assign the variables.

 

$name = $_POST['name'];

 

Where a user writes there name as something like " ''; mysql_query( "DROP TABLE *" );" ending the assignment and starting there own code.

 

So I'd have to add slashes right away?

 

$name = addslashes($_POST['name']);

 

What if I'm not printing any user input? Would I still need to check for PHP? Say...

 

if( $_POST['message'] != "" )
echo "Thanks for posting";

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.