phpBB2Refugees.com Logo
Not affiliated with or endorsed by the phpBB Group

Register •  Login 

Continue the legacy...

Welcome to all phpBB2 Refugees!Wave Smilie

This site is intended to continue support for the legacy 2.x line of the phpBB2 bulletin board package. If you are a fan of phpBB2, please, by all means register, post, and help us out by offering your suggestions. We are primarily a community and support network. Our secondary goal is to provide a phpBB2 MOD Author and Styles area.

[Solved] redirection issues


 
Search this topic... | Search phpBB2 Discussion... | Search Box
Register or Login to Post    Index » phpBB2 Discussion  Previous TopicPrint TopicNext Topic
Author Message
achaab
Board Member



Joined: 06 Dec 2012

Posts: 11



PostPosted: Tue May 14, 2013 8:43 am 
Post subject: [Solved] redirection issues

Hi guys ^^


I found a little bug with the redirection system :
Let say i have a topic with few replies in a private forum. I want to give the link of the last reply to a friend so i have have this kind of link :
http://website/viewtopic.php?p=13#13
If my friend is logged in, the link will works. If my friend is logged out, the forum will replace my link by :
http://website/login.php?redirect=viewtopic.php&p=13#13
(until here, everything is ok)
After he logged in, instead to back to the first link, the forum will send him to :
http://website/viewtopic.php?p=13
So he will be on the top of the page instead to be on the last reply.


Can someone help me to fix this ?
Back to top
Salvatos
Board Member



Joined: 19 Feb 2009

Posts: 449
Location: Québec


flag
PostPosted: Tue May 14, 2013 11:10 am 
Post subject: Re: redirection issues

I think the problem would be append_sid() (in includes/sessions.php). It adds an 'sid' parameter to the redirect URL and does not check for #anchors, so it just adds the sid at the end of the string, which breaks the anchor. In your example, the actual redirection taking place would be to
http://website/login.php?redirect=viewtopic.php&p=13#13&sid=bunchofchars
which is interpreted as if "#13" wasn't there at all by your browser (or his).

I guess the way to go would be to expand append_sid() to check if the URL includes a reference to an anchor and if so, to insert the sid before it to preserve the anchor. I'm not very good at regular expressions, though, so I'd rather let you come up with the code icon_smile.gif
Back to top
achaab
Board Member



Joined: 06 Dec 2012

Posts: 11



PostPosted: Wed May 15, 2013 7:20 am 
Post subject: Re: redirection issues

For that i know, this is the code that manage my redirection :

Code:
               if( $session_id )
               {
                  $url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'], ENT_COMPAT, 'ISO-8859-1')) : "index.$phpEx";
                  redirect(append_sid($url, true));
               }
To find it (there is a ton of redirection inside the login.php), i changed it by
Code:
               if( $session_id )
               {
                  $url = ( !empty($HTTP_POST_VARS['redirect']) ) ? str_replace('&', '&', htmlspecialchars($HTTP_POST_VARS['redirect'], ENT_COMPAT, 'ISO-8859-1')) : "index.$phpEx";
                  redirect($url, true);
               }


That changed my initial url from viewtopic.php&p=13#13&sid=bunchofchars to viewtopic.php&p=13#13
But even without the session id in the url, i have my redirection issue (that why i skipped it on my first message, sorry)

I also changed this code by
Code:
               if( $session_id )
               {
                  $url = $HTTP_POST_VARS['redirect'];
                  redirect($url, true);
               }
But same thing. That let me think that the problem is here :
Code:
$HTTP_POST_VARS['redirect']
If i edit the previous code by
Code:
               if( $session_id )
               {
                  echo $HTTP_POST_VARS['redirect'];
               }
I see
Quote:
viewtopic.php?p=13
So the problem is really here but i dont know where this variable is set icon_redface.gif
Back to top
Salvatos
Board Member



Joined: 19 Feb 2009

Posts: 449
Location: Québec


flag
PostPosted: Wed May 15, 2013 12:48 pm 
Post subject: Re: redirection issues

Okay, this took a while but I think I mostly get what is going on.
The main problem is that the fragment identifier (#sth) is not considered part of the URL and is not sent to the server. Thus any call to $HTTP_GET_VARS will ignore it, and it gets lost in a number of places in the rediretion/login process. So perhaps the only way to keep track of it would be to turn it into an actual variable/parameter. Fortunately, in your example, that's already the case because #13 is always the same as p=13. So what's left to do is reintroduce the fragment where the actual redirection finally takes place:

In login.php, around line 233:
Code:
            if(count($forward_match) > 1)
            {
               for($i = 1; $i < count($forward_match); $i++)
               {
                  if( !ereg("sid=", $forward_match[$i]) )
                  {
                     if( $forward_page != '' )
                     {
                        $forward_page .= '&';
                     }
                     $forward_page .= $forward_match[$i];
                  }
               }
               $forward_page = $forward_match[0] . '?' . $forward_page;
            }
            else
            {
               $forward_page = $forward_match[0];
            }

What we need to do is figure out the value of p=xx in $forward_page, so we can add "#xx" at the end of the string. I can't figure out the regular expression to achieve that, though. (Keep in mind that "p" should be addressed as POST_POST_URL in case you ever change the constant.)

I was trying to find another way, but the fact that the server can't tell you whether there's a fragment identifier in the URL doesn't leave many options.
Back to top
dogs and things
Board Member



Joined: 18 Nov 2008

Posts: 628
Location: Spain


flag
PostPosted: Wed May 15, 2013 12:59 pm 
Post subject: Re: redirection issues

I observe in the last code fragment Salvatos posted
Code:
ereg

As ereg is deprecated, is it possible that this has something to do with the problem?

_________________
phpBB2 will never die, I hope!
Back to top
Salvatos
Board Member



Joined: 19 Feb 2009

Posts: 449
Location: Québec


flag
PostPosted: Wed May 15, 2013 8:07 pm 
Post subject: Re: redirection issues

I don't think so (or at least it wouldn't be the only issue). I've done some tests with the fragment identifier and there's no way that it's being seen by phpBB2. No matter what this part of the code does, as far as I can see, it didn't even receive that part of the URL in the first place. That's why I suggest deriving the identifier from the 'p' parameter and not looking for the actual "#xx" in the server variable.

If you're curious, this is the quick test I made:
Code:
<?php
echo "PHP_SELF: This script is " . $_SERVER['PHP_SELF'];
echo "<br />REQUEST_URI: The requested URI is " . $_SERVER['REQUEST_URI'];
echo "<br />QUERY_STRING: This URL's query segment is " . $_SERVER['QUERY_STRING'];
?>

Output when visiting http://localhost/sometest.php?par=am#link:
Code:
PHP_SELF: This script is /sometest.php
REQUEST_URI: The requested URI is /sometest.php?par=am
QUERY_STRING: This URL's query segment is par=am


So it seems that the only way to use the fragment identifier would be to store it as an extra string, pass it along and add code to make sure it gets included in the final redirection URL. But since we're coming from a random link that could be in a post, an e-mail, etc., the only way we have to recover it is through regular expressions anyway, so I guess we might as well do it right at the end using POST_POST_URL / 'p'.

Ideally, the regex should account for the possibility that POST_POST_URL is something other than 'p', but supposing this remains a personal hack on a forum that won't change much, it would need to look for "p=" and extract however many digits come after that. That's probably the hardest part for me, but I finally figured out a workaround using intval():
Code:
$testing = preg_split("/p=/", "http://localhost/sometest.php?p=13&mode=we");
$fragid = intval($testing[1]);
echo $fragid; // 13


It turns out that using intval() on a string will take the first however many digits in the string and make them an integer. Pretty damn convenient in our situation. If you want to try that, achaab, that would be:

IN login.php, AFTER
Code:
            else
            {
               $forward_page = $forward_match[0];
            }

ADD
Code:
            // Tweak: recover #fragment identifier for redirect after login
            // Fix me: Does not account for POST_POST_URL being changed from 'p'
            if( preg_match("/p=/", $forward_page) )
            {
               $get_frag = preg_split("/p=/", $forward_page);
               $fragid = intval($get_frag[1]);
               $forward_page .= "#" . $fragid;
            }


Not tested with a forum, but if I didn't miss anything this should probably work.
Back to top
achaab
Board Member



Joined: 06 Dec 2012

Posts: 11



PostPosted: Thu May 16, 2013 3:04 am 
Post subject: Re: redirection issues

dogs and things wrote:
...
It's not on the last version of phpbb (we have a preg_match instead)

Salvatos wrote:
...
Yes it works !! Nice work, i would never find it xD
Back to top
Salvatos
Board Member



Joined: 19 Feb 2009

Posts: 449
Location: Québec


flag
PostPosted: Thu May 16, 2013 10:50 am 
Post subject: Re: [Solved] redirection issues

Ah, I'm glad it works! Such a small piece of code, but it took me long enough to figure out icon_razz.gif
Back to top
StarWolf3000
Board Member



Joined: 10 Jun 2010

Posts: 175
Location: Germany


flag
PostPosted: Fri May 17, 2013 10:27 am 
Post subject: Re: [Solved] redirection issues

You can also use the PHP function parse_url() to split the URL:
Code:
<?php
$url = "http://www.mysite.com/index.php?content=news&id=34#content-tab1";
$hashtag = parse_url($url, PHP_URL_FRAGMENT);
echo 'URL is: ' . $url . '<br />'; // output: http://www.mysite.com/index.php?content=news&id=34#content-tab1
echo 'Hashtag (Fragment) is: ' . $hashtag; // output: content-tab1
?>


Usage of parse_url() without the second param returns an array with all parts of the URL, with param you get a string with the selected url part's content.

PHP manual: http://php.net/manual/en/function.parse-url.php
Back to top
Display posts from previous:   
Register or Login to Post    Index » phpBB2 Discussion  Previous TopicPrint TopicNext Topic
Page 1 of 1 All times are GMT - 4 Hours
 
Jump to:  

Index • About • FAQ • Rules • Privacy • Search •  Register •  Login 
Not affiliated with or endorsed by the phpBB Group
Powered by phpBB2 © phpBB Group
Generated in 0.0730 seconds using 16 queries. (SQL 0.0141 Parse 0.0009 Other 0.0579)
phpBB Customizations by the phpBBDoctor.com
Template Design by DeLFlo and MomentsOfLight.com Moments of Light Logo