urls, numbers and wildcards

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gumphfy
Forum Newbie
Posts: 23
Joined: Fri Jan 06, 2006 10:18 am

urls, numbers and wildcards

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
gumphfy
Forum Newbie
Posts: 23
Joined: Fri Jan 06, 2006 10:18 am

Post 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) {
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
gumphfy
Forum Newbie
Posts: 23
Joined: Fri Jan 06, 2006 10:18 am

Post 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 !!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply