28, May 2012

Determining the URL of the current page using PHP - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Determining the URL of the current page using PHP
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Determining the URL of the current page using PHP  (Read 1955 times)
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« on: Jun 05, 2007, 02:09:23 pm »

how can i automatically write the full url of the current page the visitor is on? please help.

« Last Edit: Jun 05, 2007, 02:12:38 pm by vbignacio »


Last blog : Bingag Beach, Bohol, Philippines
Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #1 on: Jun 05, 2007, 02:18:29 pm »

i think i got it.

Code:
<?php
  
// find out the domain:
  
$domain $_SERVER['HTTP_HOST'];
  
// find out the path to the current file:
  
$path $_SERVER['SCRIPT_NAME'];
  
// find out the QueryString:
  
$queryString $_SERVER['QUERY_STRING'];
  
// put it all together:
  
$url "http://" $domain $path "?" $queryString;
  echo 
"The current URL is: " $url "<br />";
  
  
// An alternative way is to use REQUEST_URI instead of both
  // SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
  
$url2 "http://" $domain $_SERVER['REQUEST_URI'];
  echo 
"The alternative way: " $url2;
?>



Last blog : Bingag Beach, Bohol, Philippines
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #2 on: Jun 05, 2007, 02:22:50 pm »

That's right.

You are fast Wink

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Jun 06, 2007, 08:44:20 am »

Vince next time you can check the $_SERVER array Wink

Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #4 on: Jun 09, 2007, 01:52:08 am »

Vince next time you can check the $_SERVER array Wink

thanks. how?


Last blog : Bingag Beach, Bohol, Philippines
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Jun 09, 2007, 07:46:15 am »

print_r($_SERVER)

Pinoy Webmaster
.com pimp
*****
Gender: Male
Posts: 1126
6048 credits
Members referred : 0


Philippine Beaches


« Reply #6 on: Jun 09, 2007, 10:05:45 am »

oh... and you still get the full url with it? i think im gonna use this, its much shorter. thanks!


Last blog : Bingag Beach, Bohol, Philippines
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Jun 09, 2007, 10:49:10 am »

oh... and you still get the full url with it? i think im gonna use this, its much shorter. thanks!

this will only show you the values from the server array (just for learning)


Where are my glasses?
*
Posts: 21
138 credits
Members referred : 0


« Reply #8 on: Jul 20, 2007, 01:45:43 am »

vbignacio, your code does not support SSL encryption. Here's a function which supports HTTPS and using the IP instead of the hostname.

<?php

function GetURLToScript($ip = true)
{
    if ($ip)
        $host = $_SERVER['SERVER_ADDR'];
    else
        $host = $_SERVER['HTTP_HOST'];

    $port = ($_SERVER['SERVER_PORT'] == 80) ? '':':' . $_SERVER['SERVER_PORT'];
    $path = dirname($_SERVER['REQUEST_URI']);
   
    $array = explode('/', $path);
   
    $delete = false;
    foreach ($array as $key => $value)
    {
      if ((stripos($value, '.php?') !== false) || $delete)
      {
        $delete = true;
        unset($array[$key]);     
      }
      else if ($value === '')
      {
        unset($array[$key]);
      }
    }
   
    $path = implode('/', $array);
   
    $url = $host . $port . '/' . $path . '/';

    if (getenv('HTTPS') == 'on')
        return "https://$url";
    else
        return "http://$url";
}

?> 
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6753
Tags : php script code Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Determining the URL of the current page using PHP
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 07:48:37 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.