PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Wed Jul 08, 2020 12:01 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Relative Redirect
PostPosted: Tue Jul 26, 2005 6:19 pm 
Offline
BeerMod
User avatar

Joined: Tue Jan 13, 2004 5:58 pm
Posts: 2170
Location: Jax FL & Spokane WA USA


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 6:58 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
Here is a little extension:

Syntax: [ Download ] [ Hide ]
<?php

function redirect($location)

{

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

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

  $url = $prefix;

  $url .= $_SERVER['SERVER_PORT'] != $ports[$prefix] ? ':' . $_SERVER['SERVER_PORT'] : '';

  $url .= '://';

  $url .= $_SERVER['HTTP_HOST'];



  if ($location{0} != '/')

  {

    $url .= dirname($_SERVER['PHP_SELF']);

  }



  $url .= $location;



  header('Location: ' . $url);

}


Top
 Profile  
 
 Post subject: Re: Relative Redirect
PostPosted: Tue Jul 26, 2005 9:10 pm 
Offline
Tutorials Group

Joined: Sun Jan 04, 2004 11:30 pm
Posts: 2692


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 11:13 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 16, 2002 8:57 am
Posts: 1834
Location: Watertown, MA
As I posted in the original thread, you'll want to check if a session is active and if the cookie ISN'T set, if both are true you'll want to append the SID here as PHP won't append it for you inside header calls.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 7:28 am 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
modified..

Syntax: [ Download ] [ Hide ]
<?php

function localredirect($location)

{

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

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

  $url = $prefix;

  $url .= $_SERVER['SERVER_PORT'] != $ports[$prefix] ? ':' . $_SERVER['SERVER_PORT'] : '';

  $url .= '://';

  $url .= $_SERVER['SERVER_NAME'];



  if ($location{0} != '/')

  {

    $url .= dirname($_SERVER['PHP_SELF']);

  }



  $url .= $location;

 

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

  {

    $pos = strpos($url, '?');

    if ($pos === false)

    {

      $url .= '?';

    }

    $url .= session_name() . '=' . session_id();

  }



  header('Location: ' . $url);

  exit;

}?>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 1:12 pm 
Offline
Forum Regular

Joined: Sat Jul 09, 2005 10:22 am
Posts: 594
Location: Israel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 1:30 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 16, 2002 8:57 am
Posts: 1834
Location: Watertown, MA


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 1:51 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
@nielsene: Meaby i don't get it... But &amp; is only used in html :)

@pilau: it works like this:

- it adds http or https
- then it looks at the portnumber and if it's not the default 80/443 it adds the portnumber
- then we add the servername
- if the location doesn't start with / we calculate the base path and add that
- then we add the location
- then we add an eventual session id


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 1:53 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
@nielsene: i do get it ;)
Syntax: [ Download ] [ Hide ]
<?php

function localredirect($location)

{

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

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

  $url = $prefix;

  $url .= $_SERVER['SERVER_PORT'] != $ports[$prefix] ? ':' . $_SERVER['SERVER_PORT'] : '';

  $url .= '://';

  $url .= $_SERVER['SERVER_NAME'];

 

  if ($location{0} != '/')

  {

    $url .= dirname($_SERVER['PHP_SELF']);

  }

 

  $url .= $location;

 

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

  {

    $pos = strpos($url, '?');

    if ($pos === false)

    {

      $url .= '?';

    }

    else

    {

      $url .= '&';

    }

    $url .= session_name() . '=' . session_id();

  }

 

  header('Location: ' . $url);

  exit;

}

?>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 2:00 pm 
Offline
Tutorials Group

Joined: Sun Jan 04, 2004 11:30 pm
Posts: 2692


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 2:02 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 2:06 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 16, 2002 8:57 am
Posts: 1834
Location: Watertown, MA


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 2:11 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
What i meant with "i get it":

- I saw that the code only handled urls without '?'.
- So i added the case where there is already a '?' by appending an &


In case you want to do more kinky stuff, one could also consider using .


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 2:29 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 16, 2002 8:57 am
Posts: 1834
Location: Watertown, MA


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 2:36 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
I think those are 2 different levels...

- First one should use urlencode to build a valid url..

- A lot later, when it's decided to display it in a website (html) one should use htmlentities..


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 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