Forum Moderators: coopster

Message Too Old, No Replies

Checking url problem - Referrer issues

referrer

         

chrissim

2:36 pm on May 6, 2010 (gmt 0)

10+ Year Member



hi,

i have this small code that check the url contains the www. at the begining and if it doesn't it will add them manually before insert into my database. But then the problem came out if there was a subdomain existed eg http://example.com the code will add on www at it like http://www.example.com and keyed them to my database. How can i solve the problem here if such subdomain came into the picture, could we not adding those www into these subdomains.

There are millions of subdomains out there and i don't want to disregard them.


$found = ereg('\www.',$surl);
if ($found) {
$url = $surl;
} else {
$url = 'www.'.$surl;
}



Thanks

Chris

[edited by: dreamcatcher at 5:33 pm (utc) on May 6, 2010]
[edit reason] use example.com, thanks. [/edit]

Readie

3:44 pm on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The following code will match a domain, it's TLD, and any additional parameters. It will not match anything with a subdomain (that includes www) and it will not match http://

if(preg_match('/^[a-z\d][a-z\d\-]{1,62}\.([a-z]{2,4}|[a-z]{2,3}\.[a-z]{2})(\/[^\/]+)*?\/?(\.?:[\d]+)?(\?.*)?$/i', $surl)) {
// URL, no subdomains at all
} else {
// No subdomains, might not be URL
}

chrissim

12:00 am on May 7, 2010 (gmt 0)

10+ Year Member



hi Readie,

i'm getting Array recorded in my mysql table here after i used your code. The code that you provided here stated no subdomain at all but i would prefer them to be included in my record except my previous code insert the "wwww" into my mysql

Eg:

Valid Url: [crazy.site.com...]

My Code Checking Manually Added 'www' and Insert in my Mysql wwww.crazy.site.com




$found = ereg('\www.',$surl);
if(preg_match('/^[a-z\d][a-z\d\-]{1,62}\.([a-z]{2,4}|[a-z]{2,3}\.[a-z]{2})(\/[^\/]+)*?\/?(\.?:[\d]+)?(\?.*)?$/i', $surl)) {
// URL, no subdomains at all
} else {
// No subdomains, might not be URL
}


Thanks

Chris

chrissim

12:05 am on May 7, 2010 (gmt 0)

10+ Year Member



hi Readie,

i want them recorded subdomain as crazy.site.com but not www.crazy.site.com
Sorry i have a typo mistake in my previous post wwww instead of www

Cheers :)

TheMadScientist

12:22 am on May 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I definitely recommend switching your ereg() for preg() because ereg() is deprecated in PHP 5 and removed from PHP 6...

chrissim

12:35 am on May 7, 2010 (gmt 0)

10+ Year Member



hi TheMadScientist

The error still the same and getting 'Array' data recorded in my mysql table


if(preg_match('/^[a-z\d][a-z\d\-]{1,62}\.([a-z]{2,4}|[a-z]{2,3}\.[a-z]{2})(\/[^\/]+)*?\/?(\.?:[\d]+)?(\?.*)?$/i', $surl)) {
// URL, no subdomains at all
} else {
// No subdomains, might not be URL
}

TheMadScientist

12:59 am on May 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's probably because you're not specifying an array piece to store in the database, and that's how the 'matches' in parenthesized patterns are stored...

if(preg_match('/^([a-z\d][a-z\d\-]{1,62}\.([a-z]{2,4}|[a-z]{2,3}\.[a-z]{2})(\/[^\/]+)*?\/?(\.?:[\d]+)?(\?.*)?)$/i', $surl)) {
print_r($sulr);
} else {
echo 'No Match';
}

The above should give you some ideas.
To know which array piece count the ( <- in the expression.

I added a set of () around the whole pattern, because what you had matches what you are looking for, but did not store the entire pattern.

The ereg() was just a note, not a fix for anything.
I didn't look at the thread too closely earlier.

chrissim

1:59 pm on May 7, 2010 (gmt 0)

10+ Year Member



hi guys,

I need some guide or maybe show me how to construct coding on how to stop multiple hits from the same visitor per day.


if (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) {
// Is there a referrer? Is it usable? (empty?)
$ref = $_SERVER['HTTP_REFERER'];
} else {
$ref = "No referrer";
}
// Check IP address.
if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
// Again, is there an IP address and is it not empty?
$ip = $_SERVER['REMOTE_ADDR'];
} else {
// Sometimes, when going through a proxy and such, you can use this
// You can use the ternary operator to save space, etc
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = "No IP";
}
}
// Split our URL into bits.
$url = explode("/",$ref);

Readie

2:05 pm on May 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Store the visitor's IP address in a MySQL database, set up a cron job to clear the MySQL table every 24 hours.

For every visitor, run a SQL query to look for the IP address. If found: error, else: insert into the table and serve content.

chrissim

1:03 am on May 8, 2010 (gmt 0)

10+ Year Member



hi,

My referrer script keep showing hits and record them from same visitor coming from index(home page), 2nd page and so forth. Is there any way i can stop hits from generate if visitor has already landed in my index page. Can you guys show me some coding script how to implement on my code below.


if (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) {
// Is there a referrer? Is it usable? (empty?)
$ref = $_SERVER['HTTP_REFERER'];
} else {
$ref = "No referrer";
}

// Check IP address.
if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
// Again, is there an IP address and is it not empty?
$ip = $_SERVER['REMOTE_ADDR'];
} else {
// Sometimes, when going through a proxy and such, you can use this
// You can use the ternary operator to save space, etc
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = "No IP";
}
}
// Split our URL into bits.
$url = explode("/",$ref);
// Take the 3rd part of the url (the main site)
$surl = $url[2];
// Check to see if it contains the www. at the begining
$found = ereg('\www.',$surl);
if ($found) {
$url = $surl;
} else {
$url = 'www.'.$surl;
}
$query = "SELECT website, hits FROM referers WHERE website = '$url'";



Thanks

Chris

TheMadScientist

2:50 am on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hey I didn't look at your code too closely again, because I'm coding and sometimes coding for other people is a nice break from what I'm doing, but right now I can't lose my place, so my posts are about 30 seconds to a minute...

Anyway, I would check for X_FORWARDED_FOR first, because if you get the REMOTE_ADDR of the server doing the forwarding you don't ever check to see if there was a forwarded IP. You get the server doing the forwarding's IP instead of the IP being forwarded.

chrissim

3:12 pm on May 8, 2010 (gmt 0)

10+ Year Member



hi,

Anyone here can brief me the coding and also please show me to exclude my own site to be counted. i'm really sorry as i have very little knowledge on these php stuff.

Thanks