...

View Full Version : When to use Preg_match: Any Example, please!



blankskull
06-17-2010, 06:50 PM
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?

tomws
06-17-2010, 08:15 PM
Surely Google offers a few results when searching for preg_match. The documentation (http://php.net/preg_match) also has examples.

blankskull
06-18-2010, 04:40 AM
Surely Google offers a few results when searching for preg_match. The documentation (http://php.net/preg_match) 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?

tomws
06-18-2010, 04:56 AM
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 (http://php.net/manual/en/language.operators.comparison.php). Like this:

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:

$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.

Jazz914
06-18-2010, 11:03 AM
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.

blankskull
06-18-2010, 06:01 PM
$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.

blankskull
06-18-2010, 06:07 PM
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.

blankskull
06-18-2010, 07:14 PM
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.

tomws
06-18-2010, 07:28 PM
Did you consult the documentation (http://php.net/fopen)? Did you consult Google (http://www.google.com/search?hl=&q=php+file+handling)?

blankskull
06-18-2010, 09:14 PM
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>

tomws
06-18-2010, 09:33 PM
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.

blankskull
06-19-2010, 07:12 PM
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.

blankskull
06-19-2010, 07:17 PM
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.

blankskull
06-19-2010, 07:38 PM
Did you consult the documentation (http://php.net/fopen)? Did you consult Google (http://www.google.com/search?hl=&q=php+file+handling)?

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>

DJCMBear
06-19-2010, 08:06 PM
Why are you trying to read the image file and not just use the html img tag?

blankskull
06-20-2010, 05:30 AM
Thanks, after using the following code I got content of the file on the browser.

<html>

<body>
<?php

$filename = "database.txt";
$fp = fopen($filename, "r") or die("Couldn't open $filename");
while(!feof($fp))
{ $line = fgets($fp);
print "$line<br>";
}
fclose($fp);

?>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum