redirect to current page after header sent - php

My code structure is like this:
some php code here...
html
head
script
some ajax code here
/script
after running the ajax code, I want to redirect/refresh the page. How can I do it?
Thanks,

Try window.location.reload() in the ajax request success callback.

you can do it with JavaScript; place in AJax callback function:
window.location.reload(); //for refresh
window.location = "http://www.google.com/"; //redirect

Use JavaScript:http://www.tizag.com/javascriptT/javascriptredirect.php
Using window.location="URL" you can send the client to "URL".
But keep in mind that this will not work for people with JavaScript disabled (but so will you AJAX-code).

The best thing to do is to follow the ajax with a javascript redirect using document.location.href='page.html' as you're already relying on running some code in the browser.

Well it depends on what JavaScript version the browser has, but the latest versions should support:
window.location.reload(false);
Have that run on the callback from your AJAX routine.

Related

AJAX or Direct Link

What is the best way to handle Ajax vs. Full page requests with PHP ?
I mean if I click a button that calls Ajax function and loads necessary info into a div and changes the URL.
How do I make that link work if the user copy it and use later to reach the same page.
In some of the apps I separate it with a hash. Something like a router and my code goes something like this:
var preRoute = document.URL.split('#');
if(preRoute[1] != undefined) {
//ajax call
} else {
//default page
}
you can try Jquery Adress http://www.asual.com/jquery/address/,
demo here
http://www.asual.com/jquery/address/samples/state/
or
http://vietcode.vn
You could check the headers. jQuery for example will add the following key/value to the header: X-Requested-With: XMLHttpRequest for ajax calls. See also this question
Using: jquery pjax - https://github.com/defunkt/jquery-pjax
you can detect on server side if you get X-PJAX request header
And a great feature of this plugin is the history manipulation and ease of use.

Javascript page redirect from Ajax script

Due to some unforeseen reason, I cannot use a *.php extension for my page. And I need to put conditional redirection in the page.
I plan to use *.html, and javascript/Ajax to call a PHP which evaluates the condition
and accordingly sends back a redirection command.
The condition is being evaluated, but how can I give a redirection command from this internal PHP is not clear to me.
You can actually map all your .html files to .php using .htaccess.
If that doesn't work for you, your logic should be as follows:
PHP:
echo json_encode(array("redirect_url" => $url));
Javascript with jQuery (assuming you require AJAX to redirect):
$.getJSON("PHP/Controller/Url", function(data) {
window.location.href = data.redirect_url;
});
Javascript (if you simply serve a static html file)
<script>
window.location.href = "redirect_url";
</script>
You can use
window.location = "http://new-url"
or
window.location.href = "http://new-url"
In JS,
location.assign("http://www.google.com")
will redirect your current frame to Google.
So don't send redirect header in PHP, send "flags" in response body and use JS to redirect.

Refreshing a page with javascript using php header('location')

I want use a single php file to handle all of my voting requests.
Currently the script will, if siteType isn't set, forward the user and display a message. If the user has JS on then it will return a json object and ajax the page.
if(!isset($_COOKIE['siteType'])){
displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
header('Location:/');
exit;
}
I need it as that if this php code above is executed the page will reload, i assume with javascript and reading the http headers?
[Edit]
I didn't make myself clear enough, but this is a ajax request. I don't output any html. So this really is just about getting js to handle the header?
You can't Refreshing a page with javascript using php header('location')
Because, header('Location: xxx'); must be the only output of your PHP script, it is a header, you can not put it after javascript syntax
PHP
echo '<meta http-equiv="refresh" content="0">';
Javascript
window.location.reload();
Maybe this would be some solution for you,
if(!isset($_COOKIE['siteType'])){
displayMessages('bad', 'Before you can continue you must select which category you would like to be in.');
echo '<script>window.location.reload()</script>';
exit;
}

How to go to new Html page from PHP script?

After the php script is over, how do I go to another html page?
Why would you want to do that? When the page is over (which I understand as the script ended execution), it is usually sent to the client and then it can be viewed by the user. So, basically, what you are trying to do is to redirect the user after the script stopped executing.
If so, you have two solutions available:
Do not output anything, and after your script stopped executing, use the header() PHP function:
header('Location: http://example.com/some/url');
where you should replace the example URL with your own.
If you are outputting the HTML page and sending it gradually to the user, you can just put JavaScript redirection script at the end of the page (so after everything has been sent):
<script>
window.location = 'http://example.com/some/url';
</script>
Does any of these solutions work for you?
header('Location: /page.html');
Make sure you don't output anything else, then simply
header('Location: http://www.example.com/');

Obtain actual browser URL in PHP

I need to retrieve the actual URL that the user see's in their browser. I have an Ajax request running at page load. Hence, the regular $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"] expression returns the request URL of the Ajax request instead of the actual URL in the browser.
Any idea how to get this?
You could pass it up from javascript in your ajax request, using window.location.href.
Also, it's likely that $_SERVER['HTTP_REFERER'] will contain the browser's current location.
You could also try using $_SERVER['HTTP_REFERER'];. This might work, not 100% sure though.
You can't do that with server-side code, as there is no server-side variable that refers to what the client sees. The only thing you CAN see (and then again, it depends on the browser the user's using, some don't pass this info) is the HTTP_REFERRER variable. This however, is only set when a page calls another, not when users first access your site.
See this for more details.
A possible solution however, might be to use javascript function to send the browser's top URL to the server using an AJAX query, and to fire it client-side whenever a user loads the pages) you want to get this info for.
Edit: Damn, too slow, already answered!
Pass a hidden input that has the browser value set with your ajax request. Unless someone is being malicious, it should suffice.
If you do an Ajax-request, you could pass the address available through Javascripts window.location.href variable as a POST-variable with the request.
With jQuery it would be something like:
$.ajax({
url: 'your-url.php',
type: "POST",
data: { url: window.location.href },
success: function (data) {
// Do something on success
}
});
With such a request you could access the URL on the server-side with a simple:
<?php
$url = $_POST["url"];
?>
Actual Website Link in php
<?php
echo $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
Server-side languages can't see what happens after they've rendered and outputted the page.

Categories