PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jul 07, 2020 11:21 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Redirection
PostPosted: Wed Jul 27, 2005 8:03 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
The following script redirects to $location.

If $location is not an absolute URI it will generate an absolute URL.
When appropriate, the session_id will be appended.


Syntax: [ Download ] [ Hide ]
<?php

function redirect($location)

{

  $url = '';

 

  // parse of the location into URL

  $parts = parse_url($location);

 

  // lookup scheme

  if (!isset($parts['scheme']))

  {

    // choose between HTTP and HTTPS as scheme

    $parts['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https';

  }

  $parts['scheme'] = strtolower($parts['scheme']);

 

  // lookup host

  if (!isset($parts['host']))

  {

    $parts['host'] = $_SERVER['SERVER_NAME'];



    // lookup port

    if (!isset($parts['port']))

    {

      $ports = array('https' => 443, 'http' => 80);

      if (!isset($ports[$parts['scheme']]) || $ports[$parts['scheme']] != $_SERVER['SERVER_PORT'])

      {

        $parts['port'] = $_SERVER['SERVER_PORT'];

      }

    }

  }



  // lookup path

  if ($parts['path']{0} != '/')

  {

    $path = str_replace('\\', '/', dirname($_SERVER['PHP_SELF']));

    $path .= '/';

    $parts['path'] = $path . $parts['path'];

  }



  // add eventual session_id

  if (!isset($_COOKIE[session_name()]) && isset($_SESSION))

  {

    // hosts needs to be the same

    if ($parts['host'] == $_SERVER['SERVER_NAME'])

    {

      // http://host is different than https://host

      if (($parts['scheme'] == 'https' && !empty($_SERVER['HTTPS'])) || ($parts['scheme'] == 'http' && empty($_SERVER['HTTPS'])))

      {

        if (!isset($parts['query']))

        {

          $parts['query'] = session_name() . '=' . session_id();

        }

        // if there is already a session_id, don't override

        else

        {

          $pos = strpos($parts['query'], session_name() . '=');

          if ($pos === false)

          {

            $parts['query'] .= '&' . session_name() . '=' . session_id();

          }

        }

      }

    }

  }

 

  // rebuild URL

  $url = $parts['scheme'];

  $url .= '://';

  if (isset($parts['user']))

  {

    $url .= $parts['user'];

    if (isset($parts['pass']))

    {

      $url .= ':' . $parts['pass'];

    }

    $url .= '@';

  }

  $url .= $parts['host'];

  if (isset($parts['port']))

  {

    $url .= ':' . $parts['port'];

  }

  $url .= $parts['path'];

  if (isset($parts['query']))

  {

    $url .= '?' . $parts['query'];

  }

  if (isset($parts['fragment']))

  {

    $url .= '#' . $parts['fragment'];

  }

 

  //echo $url . '<br>';

  header('Location: ' . $url);

  exit;

}

?>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group