Page 1 of 1

urls, numbers and wildcards

Posted: Mon Aug 28, 2006 6:45 am
by gumphfy
I have a script which allows people to submit a link into a database, however I was to limit the urls submitted to one site. i.e. I only wanna accept urls from http://www.example.com

however, i want the script to recognize any urls begining with that address. i.e. i also want it to allow urls such as http://www.example.com/index.php?id=1234

my biggest concern is how to define any given number, ive tried:

Code: Select all

$url = 'http://www.example.com/index.php?id=' . '^[0-9]';
but that doesn't work.
basically this is what i want it to do:

Code: Select all

if ($site != $url) {
echo 'this is not the correct url';
} else {
//insert into db blah blah blah
}
How can I define any given number in an url?

TIA

Posted: Mon Aug 28, 2006 6:55 am
by volka
If you wnat to use regular expressions in php you have to use one of the regex functions, see http://de2.php.net/pcre

Posted: Mon Aug 28, 2006 7:01 am
by Buddha443556
How about something like this ...

Code: Select all

if(preg_match('/^http://www.example.com/index.php?id=\d+$/i', $url) != 1) {
    // Error stuff
} else {
    // Insert into database stuff
}

Posted: Mon Aug 28, 2006 7:09 am
by Ollie Saunders
You may want to look at parse_url() but remember
PHP Manual wrote:This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.
but it might make validation easier.

Posted: Mon Aug 28, 2006 7:47 am
by gumphfy
I keep getting unknown modifier / on preg_match


this is waht i have so far...

Code: Select all

$site = 'http://www.example.com/index.php?id=';
$pattern = '/^';
$pattern2 = '\d+$/i';

if(preg_match($pattern, $site, $pattern2, $url) != 1) {

Posted: Mon Aug 28, 2006 11:09 am
by Buddha443556
Buddha443556 wrote:How about something like this ...

Code: Select all

if(preg_match('/^http://www.example.com/index.php?id=\d+$/i', $url) != 1) {
    // Error stuff
} else {
    // Insert into database stuff
}
Oops ... :oops:

Code: Select all

$url = 'http://www.example.com/index.php?id=1223';
if(preg_match(',^http://www\.example\.com/index\.php\?id=\d+$,i', $url) != 1) {
    // Error stuff
    echo 'ERROR';
} else {
    // Insert into database stuff
    echo 'SUCCESS';
}
Notice: That the question mark and periods have been escaped (using backslashes). The delimiters have been changed to a commas to make it easier to work with URLs.

In my own defense I did use the word "like" in my original post. :-p

Posted: Mon Aug 28, 2006 11:54 am
by RobertGonzalez
Is the URL you are checking already in the database? Or are you trying to prevent multiple addresses? What exactly are you after?

Posted: Mon Aug 28, 2006 12:37 pm
by gumphfy
Basically what I'm doing is allowing people to submit a link from a certain site. The link is upon submission inserted into the database. if another person submits the same link, the script will merely add +1 to hits, making that link "more popular", the more times a link is added, the more hits it get in the db.
However I only wanna accept links from one site only, and therefore need to disallow everything else but that site.

oh and major thx to Buddha443556, you've been a great help to me !!

Posted: Mon Aug 28, 2006 12:56 pm
by RobertGonzalez
Maybe you can store domains, then store links related to the domain by id and keep track of popularity with the links table instead of the domain table.