Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts

    When to use Preg_match: Any Example, please!

    Can you please tell me why people use preg_match? Can you give me any example? Also I want to prepare a MCQ question system. In this system students will be able to take exam on 10 MCQ questions. For each question, there will 4 options. the correct option or answer will come from the correct answer_table but the other 3 incorrect options will come from other table. Can I use the preg_match to check the correct answer? Any other suggestion?

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Surely Google offers a few results when searching for preg_match. The documentation also has examples.
    Are you a Help Vampire?

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    Surely Google offers a few results when searching for preg_match. The documentation also has examples.
    Thanks for your reply. Anyway, I googled a lot and read a lot. But what I want to know is whether I can use preg_match to check the correctness of the client's password. I mean should I use it to compare the client's password input on the log in form and the password stored in the database?

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    While you could use it for a password comparison, that's really not the best idea. If you consulted the documentation, you noticed that preg_match tests for a certain pattern within a string. When comparing passwords, you don't want to test for a pattern, you want an exact match. So that means you want to use either the "equal" or the "identical" comparison operators. Like this:
    PHP Code:
    if ($userEnteredPassword == $passwordFromDatabase)
    {
      
    // do stuff

    That being said, that's still not the best way to handle passwords. Note that in the example there, the user's password as stored in the database is exposed in the application. Not good, eh? That's why you'll see a lot of login systems only try to pull the username (or other non-password fields) from the database if the login criteria matches. If there are no results, that means the login credentials were incorrect. Here's a quick simple example assuming some db stuff is already set up:
    PHP Code:
    $query 'SELECT `username` FROM `users` WHERE `username`="'.$userEnteredUsername.'" and `password`="'.$hashedUserEnteredPassword.'"';
    $result mysql_query($query);
    if (
    mysql_num_rows($result) == 1)
    {
      
    // handle login stuff
    }
    else
    {
      
    // bad credentials

    There are probably thousands of tutorials on user login systems. Look around and see what different people do.
    Last edited by tomws; 06-18-2010 at 12:31 PM. Reason: more...stupid mistakes
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    blankskull (06-18-2010)

  • #5
    Regular Coder
    Join Date
    Jul 2009
    Posts
    187
    Thanks
    16
    Thanked 8 Times in 8 Posts
    However you could use preg_match to check if the password is in the correct format, like checking if it contains the correct characters you require your users to enter. Such as:

    Lets say your only allowed: []! in your password and the user enters
    [this!ismy"pass"]

    You could use preg_match for that.

  • Users who have thanked Jazz914 for this post:

    blankskull (06-18-2010)

  • #6
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    PHP Code:
    $query 'SELECT `username` FROM `users` WHERE `username`="'.$userEnteredUsername.'" and `password`="'.$hashedUserEnteredPassword.'"';
    $result mysql_query($query);
    if (
    mysql_num_rows($result) == 1)
    {
      
    // handle login stuff
    }
    else
    {
      
    // bad credentials

    Thanks, your answer is so helpful. Any suggestion from a real person (more than a book) enhances the confidence level to a great extent. Thank you again.
    Last edited by blankskull; 06-18-2010 at 06:29 PM.

  • #7
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Jazz914 View Post
    However you could use preg_match to check if the password is in the correct format, like checking if it contains the correct characters you require your users to enter.
    Thank you for the helpful post. I am always afraid that I will be ignored by the members of this forum. But you people are really helpful.

  • #8
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Anyway, any example of fopen? I used it to open a txt file. But the browser did not show anything of the 50 lines of the txt file.

  • #9
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Did you consult the documentation? Did you consult Google?
    Are you a Help Vampire?

  • #10
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    How can I add radio button in the echo command? Please take a look at the following script:


    <html>
    <body>
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("test", $con);

    $result = mysql_query("SELECT * FROM quest ORDER BY ID DESC LIMIT 1;");

    echo "<table border='0'>
    <tr>


    </tr>";

    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";

    echo "<td> <b> Question: ". $row['ques'] . "</b></td>";

    echo "</tr>";

    echo "<tr>";

    echo "<td><input type="radio"/>" . $row['answer1'] . "</td>";

    echo "</tr>";

    echo "<tr>";

    echo "<td>" . $row['answer2'] . "</td>";

    echo "</tr>";
    }
    echo "</table>";

    mysql_close($con);
    ?>
    </body>
    </html>

  • #11
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Your quotes aren't escaped.

    You really should consult the "Help Vampire" link in my sig and see if it applies to anyone you know.
    Are you a Help Vampire?

  • #12
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    Your quotes aren't escaped.

    You really should consult the "Help Vampire" link in my sig and see if it applies to anyone you know.
    I tried to open the help vampire link. But it does not open. Anyway, I visited the site: www.tuxradar.com. It is huge. Do you have any other helpful link except help vampire? Please send.

  • #13
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    Your quotes aren't escaped.

    You really should consult the "Help Vampire" link in my sig and see if it applies to anyone you know.
    Don't get bored. php.net is a bit more technical. So it is so difficult for me to understand. In Google search there are few sites that describe the php terms in non-technical language.

  • #14
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    Did you consult the documentation? Did you consult Google?
    Hello, I tried those sites. They tell the same. I used the following code to open a text file. But in the browser it shows "resource id#3". It showed nothing of the content of the text file.

    <html>

    <body>
    <?php
    $handle = fopen("c:\\folder\\resource.txt", "r");

    echo $handle;

    ?>
    </body>
    </html>



    Again I tried to open a gif file using the following code. But I could not see any picture on the browser.

    <html>

    <body>
    <?php
    $handle = fopen("logo.gif", "r");

    echo $handle;

    ?>
    </body>
    </html>
    Last edited by blankskull; 06-19-2010 at 07:00 PM.

  • #15
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Why are you trying to read the image file and not just use the html img tag?
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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