Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
killing-hours
RavenNuke(tm) Development Team



Joined: Oct 01, 2010
Posts: 438
Location: Houston, Tx

PostPosted: Wed Apr 20, 2011 1:27 pm Reply with quote

Hey all-

Looking for a nudge.

I've got two variables that I want to compare and return a message back of either "Found" or "Not Found". I've read up on the "preg_match" & "strstr" but I'm failing to get the syntax correct or understand the way this function works. (everything i've read says to use strstr (or stristr) over preg_match as it's faster)

exampe: Find 2 inside of 1

Code:
$var1, $var2:


if(stristr($var1,$var2)) {
echo 'Found';
}else{
echo 'Not Found'
}

_________________
Money is the measurement of time - Me
"You can all go to hell…I’m going to Texas" -Davy Crockett 
View user's profile Send private message
Palbin
Site Admin



Joined: Mar 30, 2006
Posts: 2583
Location: Pittsburgh, Pennsylvania

PostPosted: Wed Apr 20, 2011 2:06 pm Reply with quote

you should be using the str functions if it is a simple comparison or search. If it is not obviously you would need to use the preg functions.

Are you looking for comments on the code? What you posted should be fine, but it is hard to say with out knowing what the variables contain. Also make sure they are not integers or arrays.

_________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan. 
View user's profile Send private message
killing-hours







PostPosted: Wed Apr 20, 2011 2:14 pm Reply with quote

That is what I was checking on as we you posted Palbin.

This function will compare a posted file type against the allowed file types.

So it would be like this.

(I'm adding spaces between the single quotes just so it's easier to read here)
$var1 = ' jpg ';
$var2 = ' *.jpg;*.png;*.gif; ';

I also didn't post the aforementioned example correctly... it should read:

Code:
if(stristr($var1,$var2) === TRUE) {

echo 'Found';
}else{
echo 'Not Found';
}


Since stristr returned the "leftovers" from the first match given the original post.

However, I still never returned "TRUE" even if I literally put the strings instead of the variables.

i.e.
Code:
if(stristr('jpg','jpg') === TRUE) etc etc


I also thought that possibly the other characters may be throwing it off... so I tried str_replace and used an array to remove those other characters... still with no luck. Seems I'm doing it right... but it's just not playing nice with me. Must mean it's something simple I'm not seeing right in front of my face. Wink

Here's the actual function in case you see something I'm missing

Code:
if(stristr('jpg' , 'jpg') === TRUE){

   echo 'Found';
}else{
   echo 'Not Found';
}



-------------

GAHHHHHHH... nvm... seems I don't need the " === TRUE" part in there. Thanks PHP.net Wink sheesh.
 
Palbin







PostPosted: Wed Apr 20, 2011 3:43 pm Reply with quote

Quote:

A single = sign is an assignment - take what's on the right as an expression and save it in the variable named on the left.

A double = sign is a comparison and tests whether the variable / expression / constant to the left has the same value as the variable / expression / constant to the right.

A triple = sign is a comparison to see whether two variables / expressions / constants are equal AND have the same type - i.e. both are strings or both are integers.


stristr does not return a boolean value (true/false) it returns a string unless it it does not find anything, and then it will return false. If you want the TRUE in there then you only want two equal signs.
 
killing-hours







PostPosted: Wed Apr 20, 2011 3:49 pm Reply with quote

If that's the case... (and this is just my mind inquiring)... how is it that I can process the "if/then" statement without an " == TRUE" in there if it's only returning back a string and not a true statement?

I removed the triple FALSE and it works exactly how I want it to... but now you've got me curious if that's the way I should be using it.

I.e.
Code:
if(stristr($var1,$var2)){

// do this
}else{
// throw an error
}
 
Palbin







PostPosted: Wed Apr 20, 2011 9:07 pm Reply with quote

When you do an if it automatically does a true false comparison. The following are false:
FALSE
0
''(empty)
The following are true:
TRUE
1
ANY THING ELSE
 
killing-hours







PostPosted: Thu Apr 21, 2011 6:53 am Reply with quote

Thank you sir... That makes perfect sense. Learn something new everyday.

Image

(Remember this ad campaign?) Wink
 
Guardian2003
Site Admin



Joined: Aug 28, 2003
Posts: 6799
Location: Ha Noi, Viet Nam

PostPosted: Thu Apr 21, 2011 3:20 pm Reply with quote

Hmm......
Just make SURE you are matching on the last three characters of the file name not anywhere within the filename so you cannot do e.g. myfilename.php.jpg.php
If this is part of an upload sequence, make sure you change the final stored filename to something different than what the original filename was.
Most hosting companies are wiser now and correctly handle different mimetypes (to prevent executing code masquerading as images) but just in case, a normal user uploading a file should never be able to guess what a direct link to the file might be.
 
View user's profile Send private message Send e-mail
killing-hours







PostPosted: Thu Apr 21, 2011 3:23 pm Reply with quote

Already have that covered. md5'ing it with a new file name Wink Only retaining the original filename in the database for easy reference.
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©