Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Couple PHP Questions


ghost's Avatar
0 0

Hey everyone I've got a couple questions for a site I'm working on.

First off, I'm trying to use preg_match to validate multiple values in an array. Basically the code looks like this:

<?php
function validate(){
   $somearray = array('this','is','an','example');
   for ($i=0;$i<=count($somearray-1);$i++){
      if (preg_match('/^(Value1|Value2|Value3)$/',$somearray[$i])){
         return true;
      }
      return false;
   }
}
?>

The only way this returns false is if the first value doesn't pass the preg_match. So it would return true if my array values where 'Value1','asdfasd','sdfasd'. Any ideas what I'm doing wrong?

My last question is about coding a chat using ajax….kind of like facebook chat. I haven't started writing it yet but I'm just trying to think ahead of how I should go about writing it.

Checking to see if users are online would be pretty easy…I'm planning on adding a column in the 'users' table called 'logged_in' and when the user logs in the value will be set to 1 and when the user logs out the value will be set to 0. But how would I be able to check if the user is idle? The only thing I can think of is storing the time the page loaded or the user sent a chat message in a $_SESSION and then calling on ajax every couple minutes to see if the user has any activity. Is this the most efficient way to do it?

Also, what do you think would be the best/most efficient way to store the conversations?…in a mysql table (I don't really know how I would go about organizing that) or just a text file? Thanks in advance


ghost's Avatar
0 0

look at your regex,

| The choice (aka alternation or set union) operator matches either the expression before or the expression after the operator. For example, abc|def matches "abc" or "def".

^ Matches the starting position within the string. In line-based tools, it matches the starting position of any line. (this is if it is outside of brackets.)

^(Value1|Value2|Value3)

So your are trying to match the starting string to either value1 OR value2 OR value 3

Obviously it passes with value1 and then garbage.


ghost's Avatar
0 0

@stdio thanks for the response…I'm not really sure if I understand what you're saying.

I am trying to match the string with multiple values. However, I am also trying to match multiple strings with my pattern.

Basically I have a checkbox with multiple values that the user can choose from. He can choose as many as he wants and I want each value that he chooses to be validated by my form. So if he were to choose Value1 and Value2 that would be fine. However, I don't want him to be able to edit any of the values in the checkbox. So say he chooses Value1 and then he edits Value2 to be markup'; DROP TABLE users/* ….I want this input to make the function return false.

The reason I have '^' which signifies the start of the string and '$' which signifies the end of the string is so that something like markup'; DROP TABLE users/*Value1 would also return false.

What I am assuming is the problem is that the return statement will end the execution of the function; so therefore, the function will not be able to check if the remaining values the user submitted are false.

Again, sorry if I'm not getting what you're saying…If its ok with you can I just PM you a link to the actual script?


ghost's Avatar
0 0

MoshBat wrote: Just post your code; words don't seem to sink in with you.

Just here to report the code as fucking posted.


Futility's Avatar
:(
80 120

A wise traveler once stumbled across this thread and promptly proclaimed

Yes, ^ signifies the start of a regular expression, and $ is its end. Thus, the regular expression is correct for that application.

The problem is that you are treating the function as if a single success validates all of the array values. That is, validation fails if ANY of them return false… but it does not succeed if any of them return true.

Revise your function to look like this:

<?php 
function validate(){ 
$somearray = array('this','is','an','example'); 
$count = count($somearray); 
 
for ($i=0;$i<=$count - 1;$i++){ 
if (!preg_match('/^(Value1|Value2|Value3)$/',$somearray[$i])){ 
return false; 
} 
} 
 
return true; 
} 
?> 

That ensures that your function will only return true if there are no fails.

Also, you need to use the count() function outside of the for loop's conditions; otherwise, it will evaluate it on each loop. Not a huge concern right now but, on larger loops, it will help. It's also best practice.


ghost's Avatar
0 0

Thanks for the post futility…code is working properly now and I have moved count() outside of the for statement.

@Moshbat/spy…the code I originally posted was almost exactly what I was going to give stdio, but with a different pattern and subjects, which I only changed for simplicity's sake. I was just going to give stdio a link to my server so he could see the code working and edit the subject values to see what returns true or false in case he didn't have his own server to try it on.

I really am quite curious to as how words don't sink into me?…nobody before Futility posted the solution to the problem. In your post you told me to look up on preg_match…which had nothing to do what was going wrong. Go you.