User Tag List

Results 1 to 3 of 3

Thread: Simple PHP Question

  1. #1
    No Products Registered

    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple PHP Question

    This is a code that I found somewhere on this site. It has to do with registering an account through a MySQL database. I want to know how I can add a line of code into here that prevents the use of numbers and symbols (so just a-z and A-Z) in the user name box. Thanks in advance!
    PHP Code:
    <?php
    if($_SERVER["REQUEST_METHOD"] == "POST") {
    mysql_connect("localhost""root""");
    mysql_select_db("DATABASE");
    // sanitize the name to make sure nobody hacks you.
    $u mysql_real_escape_string(stripslashes($_POST["u"]));
    // md5 hashes aren't vulnerable for SQL injections, so no worries there
    $p md5(stripslashes($_POST["p"]));
    $e mysql_real_escape_string(stripslashes($_POST["e"]));
    mysql_query("INSERT INTO accounts (name, password, email) VALUES('$u', '$p', '$e')") or die
    (
    mysql_error());
    die(
    "Thank you for registering.");
    }
    ?>
    <html>
    <head>
    <title>Register</title>
    </head>
    <body>
    <form action="" method="POST">
    <p>User name: <input type="text" name="u" maxlength="12" /></p>
    <p>Password: <input type="password" name="p" /></p>
    <p>Email: <input type="text" name="e" /></p>
    <p><input type="submit" value="Register Now" /></p>
    </form>
    </body>
    </html>

  2. #2
    Forum Moderator Fusion 2.5 DeveloperHTML5 Export ModuleiOS Export ModuleSWF Export Module
    DavidN's Avatar
    Join Date
    Jun 2006
    Location
    Boston, MA, USA
    Posts
    4,044
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could strip out non-alphabetic characters with the use of a regular expression replace (splitting the original expression across two lines for clarity here):

    $u = ereg_replace("[^A-Za-z]", "", $_POST["u"]);
    $u = mysql_real_escape_string(stripslashes($u));

    The ereg_replace function takes anything in the third argument (the username posted to the page) matching the regular expression in the first argument (in this case, any character that is NOT from A-Z or a-z), and replaces it with the second argument (a blank string).

  3. #3
    No Products Registered

    Join Date
    May 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. However I was looking more for a code that would prevent the user from using numbers and give him an error message if he did. With this it seems like the code will accept something like "John56" and only end up entering "John". Is there a way I can have the code tell the user than he cannot use the name? Sorry for being nooby! And thanks for your time!

Similar Threads

  1. A simple question (using PMO)
    By LavaWave in forum Multimedia Fusion 2 - Technical Support
    Replies: 6
    Last Post: 10th November 2009, 05:19 AM
  2. simple question
    By Taco in forum Multimedia Fusion 2 - Technical Support
    Replies: 10
    Last Post: 3rd August 2009, 05:33 AM
  3. Hello Please help with this simple question
    By sephetious in forum Multimedia Fusion 2 - Technical Support
    Replies: 5
    Last Post: 12th September 2008, 07:23 PM
  4. Simple question
    By ecluse in forum Multimedia Fusion 2 - Technical Support
    Replies: 5
    Last Post: 18th January 2008, 10:34 PM
  5. Simple Question
    By Peter in forum File Archive
    Replies: 4
    Last Post: 4th August 2007, 04:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •