2

I have http header string

GET /phpmyadmin/sql.php?db=monitor&table=boundries&token=08b1cd52b3bc0068ea470988e292f54d&pos=0 HTTP/1.1
Host: mylocalwebhost.net

The above is a sample string which I will recieve through a file. In each string I need to check for validity for the first line.

The 'GET' portion can be any of GET PUT POST DELETE.

How can I match the first line using a regular expression. Please some one provide me with a valid regex that I can use with php preg_match()

2 Answers 2

2

Regex

/^(?:GET|PUT|POST|DELETE) (.*?) \S+$/m

Code

<?php
$re = '/^(?:GET|PUT|POST|DELETE) (.*?) \S+$/m'; 
$str = 'GET /phpmyadmin/sql.php?db=monitor&table=boundries&token=08b1cd52b3bc0068ea470988e292f54d&pos=0 HTTP/1.1
        Host: mylocalwebhost.net'; 

preg_match($re, $str, $matches);
echo $matches[0];
Sign up to request clarification or add additional context in comments.

4 Comments

what is the part ?: in the above regex?
It makes the group "Non-capturing", so there is no backreference for that group. The middle (.*?) is backreferenced as 1.
what is the last /m means?
That's a flag for the regex engine to treat the match as Multiline which means that ^ and $ match at the start and end of a line respectively.
0

Try with this:

$string = '//phpmyadmin/sql.php?db=monitor&table=boundries&token=08b1cd52b3bc0068ea470988e292f54d&pos=0';

$parts = explode("&", $string);
$parts = array_map("trim", $parts);
foreach($parts as $current)
{
    list($key, $value) = explode("=", $current);
    $data[$key] = $value;
}

echo '<pre>';
print_r($data);
echo '</pre>';

The result is:

Array
(
    [//phpmyadmin/sql.php?db] => monitor
    [table] => boundries
    [token] => 08b1cd52b3bc0068ea470988e292f54d
    [pos] => 0
)

2 Comments

I understand to try to validate part of the url, take as reference the "token" of this string that is extracted and validated. I apologize if the answer is not the appropriate @fSazy. $data['token'];
actually the above answer I chosen is the right one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.