I did some research and couldn't find a clear answer to my problem. This is what I have:
<?php
session_start();
$gen_num = file_get_contents($_SESSION['filename']);
$inp_num = $_POST['form-input'];
if($gen_num === $inp_num){
echo "Yes! The numbers match!";
} else {
echo "No, the numbers do not match…";
}
?>
Where the 'filename' has a string of numbers and 'form-input' is carried from a previously submitted HTML form.
Why does the IF test fail when the strings are identical?
EDIT
The simple fix was trimming the $gen_num. Also, I wasn't specific enough in saying that the 'filename' was a .txt file that included a string of numbers along with some unseen special characters.
Thank you for the help!
If $_SESSION['filename'] is as you say a string of numbers then there is no file name ending.
You should make that line:
$gen_num = file_get_contents($_SESSION['filename'] . ".txt");
Or file_get_contents won't find a file with only numbers as the name and return false.
That is why the numbers don't match.
If the two values are integers I'd floor() them and use ==
And catch for 0 exception
Related
I'm using the following code to return true or false if a string contains a substring in PHP 8.0.
<?php
$username = "mothertrucker"; // This username should NOT be allowed
$banlistFile = file_get_contents("banlist.txt"); //Contains the word "trucker" in it
$banlist = explode("\n", $banlistFile); // Splits $banlistFile into an array, split by line
if (contains($username, $banlist)) {
echo "Username is not allowed!";
} else {
echo "Username is allowed";
}
function contains($str, array $arr)
{
foreach($arr as $a) { // For each word in the banlist
if (stripos($str, $a) !== false) { // If I change $a to 'trucker', it works. "trucker" does not
return true;
}
}
return false;
}
?>
This is to detect if an inappropriate word is used when creating a username. So for example, if someone enters the username "mothertrucker", and "trucker" is included in the ban list, I want it to deny it.
Right now with this code, If I just type in the word "trucker" as a username, it is found and blocks it. Cool. However if there's more to the string than just "trucker", it doesn't detect it. So the username "mothertrucker" is allowed.
I discovered that if I explicitly type in 'trucker' instead of $a in the stripos function, it works perfectly. However, if I explicitly type in "trucker" (with double quotes), it stop working, and only blocks if that's the only thing the user entered.
So what I'm seeing, is it looks like the string $a that I'm passing it is being interpreted by PHP as a double quoted string, when in order for this to detect it properly, it needs to be a single quoted string. But as far as I can tell, I have no control over how php passes passing the variable.
Can I somehow convert it to a single quoted string? Perhaps the explode command I'm using in line 2 is causing it? Is there another way I can pull the data from a txt document and have it be interpreted as a single quote string? Hopefully I'm made sense with my explanation, but you can copy and paste the code and see it for yourself
Thanks for any help!
One potential problem would be any whitespace (which includes things like \r) could stop the word matching, so just trimming the word to compare with can tidy that up...
stripos($str, $a)
to
stripos($str, trim($a))
I do not know what your file actually contains so i dont know what the result of explode is.
Anyways my suggestion is (depending on the speed you want to perform this and also the length of the banlist file also your level of banning) to not explode the file and just look into it as a whole.
<?php
$username = "allow"; // This username should be allowed
$banlist = "trucker\nmotherfucker\n donot\ngoodword";
var_dump(contains($username, $banlist));
function contains($str, $arr)
{
if (stripos($arr, $str) !== false) return true;
else return false;
}
?>
Otherwise if you are going to allow say good which is an allowed word but since it is in the file with goodword it will not (using my example), you should not use stripos but instead use your example and use strcasecmp
The $search is a string of variables made from text inputs in a form. I am looking to see if that string is found in a txt file. I think something is wrong with my regex but I am not sure.
An existing entry to the text file would look like this Title%Author%ISBN%Publisher%Year.
My issue is that when I submit the form it goes to a blank page.
elseif ($inquiry=='search') {
$file= fopen("database.txt", "r") or die("File was not found on server");
$search = "/^[$Title."%".$Author."%".$ISBN."%".$Publisher."%".$Year]/i";
//search function
// What to look for
// open and Read from file
$lines = file('database.txt');//array
foreach($lines as $line) {
// Check if the line contains the string we're looking for, and print if it does
if(preg_match($search, $line)) {
echo $line;
} else {
echo "Search not found";
}
}
}
fclose($file);
}
You have to be aware of two things first.
The special $ char in PHP is used to denominate a variable.
When you inject a word with a preceding $ in a double quoted string, this is treated as a variable name and the variable tries to expand itself.
I'm mentioning this because of this line:
$search = "/^[$Title."%".$Author."%".$ISBN."%".$Publisher."%".$Year]/i";
So, my best guess there is that you are trying to use and expand the variable names. So, if that's the case you are ok on your intention.
But be aware you have a missmatched and no closed " after the Title.
Also remember the $ has a special meaning in regular expressions, and they are usually used to try to match the "end of the line".
Note: Your script is probably dying due to a missing " after the Title.
Although a $_FILES array is not shown, this is intended for file upload, I have had problems with file uploading so I was not able to get the [type] part of the $_FILES array yet... but this is just a simple problem of why isn't this function working...
<?php
function getExtension() {
// global $testFile;
$extension = "./mp3/"; // also tried simply mp3 without the forwards slashes
$testFile = "song.mp3";
if(preg_match($extension, $testFile)) {
echo "match found";
}else {
echo "no match found";
}
}
getExtension();
?>
Do this:
$extension = "/mp3$/";
or
$extension = "/\.mp3$/";
ONLINE EXAMPLE
Reason being, you need the delimiters to begin and end your expression with (those are the slashes, though the can be any character) and can't have anything ahead of or behind them. The "$" will mean, find the string between the delimiters at the end of the string.
You can keep the period by escaping it (or else it will mean any character once, meaning testmp3 would be a match).
But really the best answer is as suggested in the comments - php's pathinfo() - since you are parsing a filename. Though in certain cases, you may want to do other tests for security, like check the mimetype.
If you want to keep the period, make sure you escape it.
$pattern = "/\.mp3$/";
There is a mistake in your Regular Expression :
"./mp3/" => "/\.mp3$/"
Result :
<?php
function getExtension() {
// global $testFile;
$extension = "/\.mp3$/"; // also tried simply mp3 without the forwards slashes
$testFile = "song.mp3";
if(preg_match($extension, $testFile)) {
echo "match found";
}else {
echo "no match found";
}
}
getExtension();
?>
ONLINE EXAMPLE
i have question on string comparison i have
if($decryptedname == $plain)
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}
in a for loop to go through a text file, decryptedname contain a string "Lee" and plain contain the data in my text files and printing line by line, since both of it contain a field with the value "Lee" i assume it should match and print success as well as the $decryptedname but it's not, below is the copy and pasted result, where lee is $decryptedname and those others are echo by $plain thru a for loop
Lee
Decrypted name is Lee
Decrypted email is mjlee181#hotmail.com
Decrypted message is testing
<?php
if (strcasecmp($decryptedname , $plain) == 0) {
echo "success <br/>";
echo $_SESSION['decryptedname'];
?>
you can try this .May it help you
There are chances for whitespace getting added when you load contents from a text file , so in that case always do a trim() and then do the comparison.
Like this..
if(trim($decryptedname) == trim($plain))
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}
or if you are trying to check if the string contains in another string... you should be doing stripos() instead.
if(stripos($decryptedname,$plain)!==false)
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}
The above mentioned problem may be caused due to the '\n' present at the end of the string.
If the string "Lee" is present at the end of the line in the text file, it may automatically be appended with '\n' in the end. So if you remove '\n' from the end it might work fine. you can use the following function to do that:
$plain=str_replace("\r\n","",$plain);
How would I go about searching/matching for a paticular set of a characters in a filename, for example 3XYTPRQgz.pdf is the filename, I need to search for '3XYTPRQ', then if this string is found I simply want to output 'job completed', if its not there it will be set to queued. ( I want to do this for more than one file).
My thoughts on how to do this is, (I am struggling on the matching of the string part) :
<?php
if(match("7digitnumber) //then < not sure what function to use any tips?
if file_exists($7digitnumber/filename)
{
echo "completed";
}
else
{
echo "queued";
}
?>
Thanks for any help.
If you simply want to find out if a given string (your case "3XYTPRQ") is part of a longer string ("3XYTPRQgz.pdf") you can take a look at strstr.