Jump to content

[SOLVED] How to use strip tags?


cs.punk

Recommended Posts

I got the following code :

$username = strip_tags($_POST['username']);
   $password = strip_tags($_POST['password']);
   $email = strip_tags($_POST['email']);
   
   $username = strip_tags($username,"<i></i>");
   $password = strip_tags($password);
   $email = strip_tags($email);

 

But when I later put each varible into the database, it still shows up with <h1> if I type it...

Link to comment
Share on other sites

<?php
if (isset($_SESSION['user']))
{echo "<p align=\"center\" class=\"paragraph\">You are already logged in. Do you maybe want to log        out? If so <a href=\"logout\">click here.</a>";
}
else
{if (isset ($_POST['username']) && ($_POST['password']) && ($_POST['email']) )
  {$username = strip_tags($_POST['username']);
   $password = strip_tags($_POST['password']);
   $email = strip_tags($_POST['email']);
   
   $username = strip_tags($username,"<i></i>");
   $password = strip_tags($password);
   $email = strip_tags($email);
  
   $con = mysqli_connect ("$dbhost","$dbuser","$dbpass","$dbname")
          or die ("Couldn't connect to server");

   $sql = "INSERT INTO users (username, password, email)
	  VALUES
	  ('$_POST[username]', '$_POST[password]', '$_POST[email]')";
   mysqli_query($con,$sql);

   echo "Congratulations you have sucsessfully registered you can now login!";
}

Link to comment
Share on other sites

You're inserting the unmodified values in $_POST.

 

Change:

<?php
   $sql = "INSERT INTO users (username, password, email)
	  VALUES
	  ('$_POST[username]', '$_POST[password]', '$_POST[email]')";
?>

to

<?php
   $sql = "INSERT INTO users (username, password, email)
	  VALUES
	  ('$username', '$password', '$email')";
?>

 

You should also use mysql_real_escape_string on those values also:

<?php
   $username = mysql_real_escape_string(strip_tags($_POST['username']));
   $password = mysql_real_escape_string(strip_tags($_POST['password']));
   $email = mysql_real_escape_string(strip_tags($_POST['email']));
?>

 

Also, you're "if" statement is wrong. It should be:

<?php
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email']))
?>

 

 

Ken

Link to comment
Share on other sites

Well for one... You don't need to put closing tags in the second argument for strip_tags. So you can leave out </i>

 

Second, this is not related to your question, but..... You should use htmlspecialchars and mysql_real_escape_string on any text strings you plan on inserting into your database. Someone could easily use a register form with that code you posted to inject anything they want into your database.

 

edit-- Ya ken found it

Link to comment
Share on other sites

:)Sorry I was under the impression that the actual $_POST['username'] would be MODIFIED by it. Thanks for that :) And with the ,"<i></i>" i was just seeing if there would be a difference lol.

 

Thanks allot zanus, kenrbnsn and charleshill! :):D

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.