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
https://forums.phpfreaks.com/topic/38519-when-to-add-slashes/
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
https://forums.phpfreaks.com/topic/38519-when-to-add-slashes/#findComment-184853
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.