What is this called in PHP?

Truffles

New member
Apr 20, 2009
333
3
0
Not sure what this type of function is called and it is making it impossible for me to Google what I'm trying to accomplish.

Let's say I'm doing some PPV on Media Traffic and I can $_GET the target_passthrough variable that is passed through the URL. Depending on how I'm targeting things, it could be a mix of formats such as:

.targetsite.com
targetsite.com
news.targetsite.com
anothertargetsite.com/randomsubdirectory
yetanothersite.com/randomsub/index.php?p=test

I'd like to be able to consistently pull the root domain from that target and echo it out into my creative to do something like: "Welcome Targetsite.com User."

Any idea what types of functions would be involved so I can go read up on them? One component is pulling out whatever is after the initial "." and then including the TLD but cutting off everything after. I'd then like to capitalize the first letter of the domain and I don't know if that is a separate function or not (probably is).

If this is an easy thing to code and you feel like being nice, feel free to drop some code, otherwise I'd just appreciate some info on what the functions involved are called so I can go research this and learn more about it. Gracias.
 


welcome <?php echo $subid; ?> visitor

that would just do the domain, if you want to get the www. you need some javascript I think. I might have it.
 
put this in an empty php file (or otherwise remove the open/close tags) -
if you don't know where they are coming from you can use this to see what it looks like and adjust accordingly:
Code:
<?php echo $_SERVER['HTTP_REFERER']; ?>




if you do know where and have control over where they are coming from, you can put the following into a hidden form input field and will return the url of the webroot(plus any subdirs you may be using).

form action would be the url of your target;
this is what would go in the html code:
Code:
<input type="hidden" name="nameofthefield" value="<?php echo 'http://' . $_SERVER['HTTP_HOST'] . '/plus/whatever/else';?>" />

if you're using GET, the value will be stored in $_GET['nameofthefield'] on the target page.
 
The domain can be found in $_SERVER['HTTP_HOST']; that you're currently on. If you want another spot grab it from $_GET or wherever.

If you want the TLD and the domain (given it's not a .co.uk or any two part TLD) you would do something like this:

$domain = implode('.', array_slice(explode('.', $_SERVER['HTTP_HOST']), -2));

Change it to -3 if you want to have it on a two part TLD.

Then you probably want to $domain = ucwords($domain); to make the first char uppercase.

If you want to deal with something like news.blah.com/crap/crap/crap.php then you would:

$somevar = $_GET['somevar'];
if (strpos($somevar, '/') !== false)
$somevar = substr($somevar, 0, strpos($somevar, '/'));
$domain = implode('.', array_slice(explode('.',$somevar, -2));

Or a regex:

preg_match('/^(?:.*?)?\.(.*?)\.(.*?)([\/|$])/', $_GET['somevar'], $match);
$domain = $match[1].'.'.$match[2];
 
So parse_url gives me the www. when that exists, and returns null when the target is just example.com.

Not really sure how to implement your code with this Insomniac as I'm not pulling this from the server, I'm pulling it from a global variable called target_passthrough (which is the Media Traffic syntax for that).

Been putzing around with this all afternoon and I have no idea what the heck to do.
 
Replace $_SERVER['HTTP_HOST'] or $_GET['somevar'] with $_GET['target_passthrough'] then.
 
parse_url requires http:// for parsing URLs like you need:

// ex: pass through www.domain.com/some/site/text.html?asdfasdf=1
if(!strstr($_GET['target_passthrough'],'http://')) {
$fixTarget = "http://" . $_GET['target_passthrough'];
} else {
$fixTarget = $_GET['target_passthrough'];
}
$fixTarget = ucwords(parse_url($fixTarget)); // result: Domain.com
 
Tried the various suggestions here and none were quite doing the trick for all instances. Finally came up with something that more or less works. Two exceptions...doesn't remove subdomains, and doesn't work if your target is just a path like "/subdir/pageexample.html"

Anyway, feel free to use or critique--not much of a coder and it took me about two hours to figure out which functions would do the trick, but at least now I know how to do it. Just save this to a blank .php file and then call it with an include.

Code:
<?php 
        $url = $_GET['target_passthrough'];
        if(stristr($url, 'http') == TRUE)
        {
            $url = parse_url($url, PHP_URL_HOST);
             }    
        $url = str_replace('www.', '', $url);  // Removes the www
        $url = explode('/', $url, 2);  // Isolates the domain from the path
        $urlsplit = str_split($url[0]); // Splits each character in the domain into a field of an array
        if($urlsplit[0] == ".")  // Checks if first character is a period and if so removes it
            {
                $urlsplit[0] = str_replace('.', '', $urlsplit[0]);}
        echo ucwords(implode('', $urlsplit));
        
        ?>
 
Tried the various suggestions here and none were quite doing the trick for all instances. Finally came up with something that more or less works. Two exceptions...doesn't remove subdomains, and doesn't work if your target is just a path like "/subdir/pageexample.html"

Anyway, feel free to use or critique--not much of a coder and it took me about two hours to figure out which functions would do the trick, but at least now I know how to do it. Just save this to a blank .php file and then call it with an include.

Code:
<?php 
        $url = $_GET['target_passthrough'];
        if(stristr($url, 'http') == TRUE)
        {
            $url = parse_url($url, PHP_URL_HOST);
             }    
        $url = str_replace('www.', '', $url);  // Removes the www
        $url = explode('/', $url, 2);  // Isolates the domain from the path
        $urlsplit = str_split($url[0]); // Splits each character in the domain into a field of an array
        if($urlsplit[0] == ".")  // Checks if first character is a period and if so removes it
            {
                $urlsplit[0] = str_replace('.', '', $urlsplit[0]);}
        echo ucwords(implode('', $urlsplit));
        
        ?>

If you're using stristr() to check a string, I think you should check != false as opposed to ==true. It may seem like they would be logically equivalent, but stristr() returns either a string(if it finds the matched substring) or boolean false (if it doesn't).

You're also almost to the point where you would need to use regex, otherwise, you could break it down into cases:

case: relative uri (check for leading slash)
case: absolute URL that contains"www"
case: absolute URL that does not contain "www" (for subdomains)
case: absolute URL that does not contain "www" (as in "http://domain.com")
default: absolute URL with "www".

and possibly another case or 2 I haven't thought of.
 
If you're using stristr() to check a string, I think you should check != false as opposed to ==true. It may seem like they would be logically equivalent, but stristr() returns either a string(if it finds the matched substring) or boolean false (if it doesn't).

You're also almost to the point where you would need to use regex, otherwise, you could break it down into cases:

case: relative uri (check for leading slash)
case: absolute URL that contains"www"
case: absolute URL that does not contain "www" (for subdomains)
case: absolute URL that does not contain "www" (as in "http://domain.com")
default: absolute URL with "www".

and possibly another case or 2 I haven't thought of.

Thanks for the thoughts about cases...that makes sense and I may expand in the future if this version causes a performance hit. I tried to avoid regex as that will probably take me a whole night to learn and I wanted to get this live.

Can you clarify what is bad about your first point returning a boolean false? I guess I don't understand how that could cause an undesired outcome with my if statement.
 
suppose that
Code:
$url = "http://www.domain.com";

then
stristr($url, 'http') will return :
"://www.domain.com"


if
Code:
$url = "www.domain.com";

stristr($url, 'http') will return boolean false

so even if 'http' was in $url, this block would never be reached
Code:
        if(stristr($url, 'http') == TRUE)
        {
            $url = parse_url($url, PHP_URL_HOST);
        }

because it's really asking
Code:
if("://www.domain.com" == TRUE)



but you can flip it to be
Code:
        if(stristr($url, 'http') != FALSE)
        {
            $url = parse_url($url, PHP_URL_HOST);
        }

and if 'http' was in $url, then you would get the desired effect, because stristr($url, 'http') returns something other than FALSE.