Redirect to 2 pages - php

I have a PHP page and I want to redirect it first to a page (eg google.com) then to another page (eg bing.com).
To do this I'm using the following:
header('Location: http://google.com');
header('Location: http://bing.com');
The problem is that the script stops at the first 'header'.
Is there a way to make this?

I'm afraid the behavior as you've described it isn't possible. Exactly how would you expect this to work? How can a single browser window go to two pages at the same time? After all, the browser can't interpret your response headers until it receives the response. So there's no step in between the two lines of code you show, the browser receives them both in the same response.
Thinking in terms of the request/response nature of the web, re-consider what you're trying to do in order to meet the need you're addressing. You stated that:
I want to redirect it first to a page (eg google.com) then to another page (eg bing.com)
There's an order of events there:
User requests your page.
Your page responds with a redirect to Page 1.
User requests page 1.
Page 1 responds.
User requests Page 2.
Page 2 responds.
The step where you're looking to interject is between 4 and 5 above. Naturally, you can't modify or in any way control Google's response so you can't have a Page 1 that you don't control respond with a redirect to Page 2. (Indeed, even if they did, it's either content or a redirect... not both.)
Off the top of my head there may be a workaround that might fit your needs. You could use frames to keep the user on your page while showing them the content of these other pages. Within your parent frame, which you would control, you can use JavaScript to set the various timers and other events which would direct the user from Page 1 to Page 2. (Is it instant? If so, why bother with Page 1 at all? Is it after a few moments? What should cause the redirect from Page 1 to Page 2?)

Well, maybe you would like to open 2 page at the same time.
You can control it by javascript.
Something like following code:
var open_link_google = window.open('','_parent');
open_link_google.location="http://www.google.com";
var open_link_yahoo = window.open('','_blank');
open_link_yahoo.location="http://www.yahoo.com";

Something like following code:
var open_link_google = window.open('','_parent');
open_link_google.location="http://www.google.com";
var open_link_yahoo = window.open('','_blank');
open_link_yahoo.location="http://www.yahoo.com";

Related

hide url in navigation bar

I have different pages and scripts on my website. I want to show 1 URL in the adressbar for all the pages.
I have pages like:
www.example.com/index.php
www.example.com/map1/index.php
www.example.com/map1/map2/index.php
www.example.com/map1/map2/map3/index.php
I want that all these URLS should be shown like:
www.example.com
I have expirimented with my .htaccess script, but I cant get this working.
Can anybody help me with this problem? How can I show all the URL`s on my page like "www.example.com".
You could make www.example.com/index.php (which would be the default for www.example.com) be the only page that actually gets loaded fully, with all the others simply providing content to be loaded via ajax.
You could also make www.example.com/index.php contain only an iFrame or a frameset, such that clicks to other locations would only be to the nested frame, leaving the address bar always at www.example.com
You could also try taking "map1" and "map2" etc out of the URL and use them instead as post variables, or $_SESSION variables, or what have you.
Hide the Target URL of a Link in Status Bar
There are some instances where you have redirect the user through one page to get them to another page. There is a way to do this stealthily - without letting the user know that there was a redirect. Yes - it sounds evil - but it don't have to be. Say you have a click tracking software - you have to track each click the users make. To do that you need a redirecting page what will track the clicks. Hopefully, the following illustration will make things clearer...
Current Page->Page with the click counter->Destination Page
You don't want the user to see that you are passing through the middle page. Usually, the URL will flash in the address bar for just a second(or less) - so we don't have to worry about that. We just have to prevent the URL from appearing in the status bar when the user hovers over the link.
There are three methods to do this...
Change the status text
Hijack and stop the click event and redirect
page.
Make an Ajax call on click event.
Changing Status Text
This is the old method. This uses the window.status property to show a different URL to the user. Simple and easy method - but it rarely works now a days. This method has been abused by malicious sites a lot - so most browsers have disable this option. In Firefox, you can find that option at Tools -> Preferences -> Content -> Enable Javascript(click on the 'Advanced' Button) -> Change status bar text. If that's checked, you can use window.status to change the status bar text. But its disabled by default.
But if you still want to use this method(not recommended), this is how to do it...
<a href="click_counter.php?redirect_to=http://www.google.com/"
onmouseover="window.status='http://www.google.com/';return true;"
onmouseout="window.status='';">Go To Google</a>
Hijacking Click Event
In this method, when the user clicks on the link, the script captures the click event and stops it. This will prevent the browser from opening up the target page. Then we use location.href to go to the new page. Sample code below...
HTML Code
Go To Google
Javascript Code
<script type="text/javascript">
function init() {
document.getElementById("google-link").onclick=function(e) {
e=e||window.event;
stopEvent(e);
location.href="click_counter.php?redirect_to=http://www.google.com/";
return false;
}
}
window.onload=init;
</script>
Ajax Method
This is for all you web 2.0 fans. Ajax method simply makes a call to the counter server side script on the click event. This is perhaps the best method of all - as the counter URL doesn't appear at all. Needless to say, the server side script used here will be different from the one used in the other methods - there is no redirection here. The code is very simple as well...
HTML Code
Go To Google
**Javascript Code**
<script type="text/javascript">
function init() {
document.getElementById("google-link").onclick=function(e) {
jx("counter.php?url="+escape("http://www.google.com/")); //Use your favorite ajax library here.
}
}
window.onload=init;
</script>

PHP MVC Append URL parameters without page reload?

I am working on a site which has an animated background, the animation is powered by javascript and therefore when the user enters and submits a search it would be great if the animation were to continue smoothly whist the search was completed and results appended to the DOM.
I have managed to get to the point where the results of the search are returned to JS from an ajax request although inherently the url of the ajax request is different from the url currently displayed in browser.
So my goal is for the user to come onto the site at say, www.example.com/public/home/search
They type something into text input and press search, the url changes to something like
www.example.com/public/home/search?q=some+search+query or
www.example.com/public/home/search/somesearchquery or
www.example.com/public/home/search/#somesearchquery, etc.
but the page state remains the same, the results are appended to the DOM and no full reload occurs.
Returning to a url like the one above should load the page and send the query automatically, returning the page with the results already appended.
I don't know weather this is possible, with or without obeying the MVC pattern.
I am not using any mvc framework and would like to avoid it if I can but instead using a bare bones system similar to the one found at. https://www.youtube.com/watch?v=OsCTzGASImQ
Any ideas, suggestions, alternatives?
There are several answers here for "change URL without reload", for example: Modify the URL without reloading the page .
So, I think you just have to implement some solution from one of these answers and run some javascript that change the page.
You have to be careful, because the modified URL must to load the same version of the page the user is seeing after the changes caused by the javascript. Otherwise, if the user copy and paste the URL, he will not be happy.
One way to archive it is to create the javascript function that "updates" the page without reload it based on the text input (let's say, f). Then, if the user try to access directly the page
www.example.com/public/home/search?q=some+search+query
your server side code just return the page search with a call to this javascript function at the end, like that:
f("some search query")
so, this function will update the page and the final effect will be the same as the user just enter in the page and after tries to type some search query.
(Note that, this function f may be used in both cases: when users type the text to search and when users just paste the entirely URL).

Creating "came-from" navigation - PHP

I was wondering, how is it possible to get example the last X number of pages a user came from on my site?
I am creating a navigation, so the user easily can see the X number of previuos pages he visited on my site only.
I just don't know how to do this. Is there any function in PHP to obtain this?
Thanks in advance.
I'm assuming you mean the last X pages on your site only, and that you know how to use sessions and arrays.
In this case you could initialize a session for the user, then every time the user visits a page, append the URL or some identified of that page to the array. If there's more than X pages in the array, additionally remove the first one.
Then you could just get the contents of the array, parse them into a bunch of links and show them on your site.
In case you meant the pages before entering your site, you can only get the latest one via the referer header. Some browsers might be configured to not send the referer header, however, so there really isn't a way of accomplishing this properly.
<?php
session_start();
/*
Put here any logic that could result in a redirect, to avoid useless records
...
*/
$_SESSION['history'][] = $_SERVER['REQUEST_URI'];
$_SESSION['history'] is now an indexed array with all of the user's history on your website.

Print several different pages PHP

I want to have a button "Print all reports" which will print 5 different URLs at once. Is there a way to do it without making it redirect you to a new page I will have to create with all the code again for each of the URLs and window.print() it?
I think the way you have exposed is good from every angle. Have a different page which prints all code of 5 URLs and call window.print() function to open the print-dialog.
If not, you will have to use ajax to fetch the output of all 5 different URL's and dump it on the same page where the user clicked the button. But, I would oppose this approach since it loses the ability to go back to the page and user could be deceived if they bookmark it.
Well if that's what you want, you can AJAX content of all the five pages into one page and once it's all ready, you can print the single page. If you use jQuery, it will look like this :
(
function(){
$.ajax({url:"url1", success:function(resp){
$("#mydiv").html(resp);
//make other similar calls for rest of the pages
//and in your last callback, call window.print()
});
})();
Even if you don't want to use jQuery, you can do it with raw ajax.
using just a browser you write a script that generates 1 url out of 5 urls, and separate the html from each of the urls with this.
<DIV style="page-break-after:always"></DIV>
and then the user can just print out your 1 url and have 5 urls of information.

PHP save a page then redirect to previous page(ajax)

I would like to ask for how to redirect to previous page (ajax paging).
Example,
Let's say currently I am in page 5, then I click on one record, after I edit it, I would like to go back to that page 5 not page 1.
The problem is my paging is using ajax,
http://domain/photo/#5
I try to use $_SERVER[http_referer], but I only get http://domain/photo , I not able to get # and the value.
What is the best way to redirect back to the previous paging. My last choice is using $_SESSION. Hope I can get better answer here.
Thx.
Like Lauri says, you need to manage this at the client-side using Javascript. Basically you need to implement some sort of client-side browser history. Just look at how Facebook loads in photos, but still lets you use the back button.
There are various libraries available to help you:
JS History Frameworks

Categories