0

We have a lot of redirects in our system which works with php. But its quite difficult to remove them, because the core of the system needs them.

So, is there a possibility to redirect with php without to inform the client while processing the second page and deliver only the second pages content and its new url?

Example:

The clients requests a page named page1.php. The server processes this page and realises while processing that a redirect to page2.php is needed. Instead of redirecting to page2.php the server runs the content of page2.php and send the page2.php content to the client. Also the client should be informed that the actual URL is page2.php.

bitrevolution
  • 437
  • 6
  • 16
  • 1
    Not sure if applicable in your case but take a look at [the html5 history api](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history) – Patsy Issa Mar 28 '14 at 08:35
  • Just request the data from page2.php instead of page1.php. Without knowing exactly how your script works, this question is impossible. If page1 == page2 but a different database call, easy, just update the database call. If page1 != page2 in the slightest, but is a content body for a theme, you could just `include` the page2 file in place of page1. Without knowing, it's impossible. – David Mar 28 '14 at 08:41
  • Please state explicitly the reason you want to do that, e.g. is it a mobile app and the added latency is a problem? Visual flicker when navigating? "Just want to limit round trips because: awsome"? – AD7six Mar 28 '14 at 09:04
  • i'd like to reduce the answer time of the website. If the client has to load two websites every time someone is saving a new item and redirected to the new "edit"-site its quite annoying. We use zend framework. – bitrevolution Mar 28 '14 at 11:19
  • What you're asking to do will have no measurable effect - you should instead identify and optimise the bits of your app taking the most time which from what you describe is the time necessary to render the edit form. Don't waste time optimising something that is only responsible for 1% of "the problem". – AD7six Mar 28 '14 at 12:21

4 Answers4

2

You could use echo file_get_contents($path_to_page2); to get and echo the file contents and then put an message somewhere like echo 'The actual page is page 2 and can be found....

$actual_path = '...'; // get this value by a database request I think?
$current_path = ltrim('/', $_SERVER['REQUEST_URI']); // for example (I strip the '/' character because I think you won't use it in the database(?) either.)
if ($actual_path != $current_path) {
    echo file_get_contents($actual_path);
    echo 'The actual file can be found at ...'.$actual_path;
    exit;
}

If you would want to really redirect, just use this;

$actual_path = '...';
$current_path = ltrim('/', $_SERVER['REQUEST_URI']);
if ($actual_path != $current_path) {
    header('Location: '.$path);
    exit;
}

The exits are there to stop all other rendering of the page. Place the latter code at the top of the page when no content is displayed to the user yet to avoid errors.

user3360311
  • 74
  • 1
  • 14
1

It sounds like what you should be fixing is probably the redirects in the first place.

But to stay on topic, if what you want to get rid of is the flicker between the first page loading and the second one (which is the final destination), which is what I assume you have now, you can make an AJAX request for the content instead.

It's hard to give specifics since you don't share any details about your architecture, but it's probably going to be a big refactoring. Getting content via AJAX and displaying it is easy, even more so if you use one of the multiple frameworks available. Changing the URL via Javascript is also doable in all modern browsers, refer to this other SO question: Modify the URL without reloading the page

Good luck! If done properly, you could get something good out of it - we're actually doing this at Facebook for quite a while (maybe you can find some newer presentations). The React framework could help for the frontend rendering side of things.

Community
  • 1
  • 1
Claudiu
  • 3,261
  • 1
  • 15
  • 27
  • I don't think you're answering the question - ajax isn't fundamental to what's asked, and with no server side changes would still result in e.g. POST page 1 -> 301 page 2 -> GET page 2. – AD7six Mar 28 '14 at 08:52
  • Why do you think that? "So, is there a possibility to redirect with php without to inform the client while processing the second page and deliver only the second pages content and its new url?" - he wants to get rid of the chain of redirects and get to the last one. – Claudiu Mar 28 '14 at 08:54
  • `But its quite difficult to remove them, because the core of the system needs them`. So dealing with redirects is "difficult" and your solution is to re-design the app, front and back probably, completely. I don't disagree with the advice, I just don't think it helps the OP or _directly_ answers the question. – AD7six Mar 28 '14 at 08:57
  • Saw your edit. This would allow him to get what he wants without making as many changes. There's no silver bullet. It's easier to change the redirect code to serve a payload than it is to change the redirect code to return his final redirect. He could also use curl or something to fetch the page in advance and check HTTP status for example, but that's even crazier. It's not ideal, but it seems like a quicker way to patch it, the fix is to not have those redirects there in the first place. Feel free to edit my answer if you want to. – Claudiu Mar 28 '14 at 08:59
  • 1
    I think the answer is fine, I am _guessing_ it's out of scope for the OP, not necessarily future readers. In any case - I asked the OP to clarify their reasoning as it may influence things. +1. – AD7six Mar 28 '14 at 09:08
  • I'll keep an eye out for any updates. It's hard to give any realistic solution without any more info. – Claudiu Mar 28 '14 at 09:09
-1

Nevermind my previous answer, it seems I misread.

You can do what you want using PHP and HTML5 (with Javascript).

You'll first need to use ob_start(), ob_get_contents(), and ob_end_clean() for that. (Just so you won't throw the content before actually being in the script you want.

Then, you'll need to use HTML5 browser history tool.

So in your php :

page1.php

<?php
ob_end_clean();
ob_start();
echo "hello"

if(1){ //oh I need to load page2.php !
    $loading_script = '';// use this to suit your needs https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history
    include('header.php');
    include('page2.php');
}

$html = ob_get_contents();
ob_end_clean();
echo $html;
?>

page2.php

<?php
ob_end_clean();
ob_start();
echo "hello 2"

if(0){ //oh I need to load page1.php !
    $loading_script = '';// use this to suit your needs https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history
    include('header.php');
    include('page2.php');
}

$html = ob_get_contents();
ob_end_clean();
echo $html;
?>

header.php

<html>
    <script>
        <?=$loading_script?>
    </script>
    <body>

Beware of infinite loops between including your php scripts though.

Loïc
  • 11,804
  • 1
  • 31
  • 49
  • Isn't sending header('location: ...') first a response back to the client and the client afterwards sends a new request to page2.php? I'd like to have it without any second request. – bitrevolution Mar 28 '14 at 08:40
-1

This is how I did it, somewhere in the function if there is a need to redirect

    $redirect_page = "link.php";
    header('Location: ' . $redirect_page);
    exit();
Saadia
  • 856
  • 1
  • 12
  • 32