I need to pass the URL of a page to a PHP POST function which will return a document. I was wondering if it is possible to pass in the URL via a function or will it have to be stored in a variable? Or is it even possible to do this? I have this function to retrieve the URL of the previous page:
$(document).ready(function() {
var referrer = document.referrer;
});
And this is the PHP POST method which does not work:
$_POST['http://<THE URL I AM SENDING THE REQUEST TO>&url=<THE URL OF THE PREVIOUS PAGE>'];
I had it set up like:
$_POST['http://<THE URL I AM SENDING THE REQUEST TO>&url=referrer'];
But it would not run. Does anyone know how I can get this to work? Thanks
Related
I've tried to determine the current page in functions.php with an if (is_home()) function, because I only want to run the specific ajax request on my homepage, but it always returns false and if I request the current URI, it says /wp-admin/admin-ajax.php/wp-admin/admin-ajax.php, the URI I actually request in my JS Ajax function.
So how can I only request my current page instead of the Ajax request?
Thanks for any help!
In the javascript
var pathname = window.location.pathname;
Then you can pass it with the rest of your data to the php function.
How can I do PHP redirect header in Laravel 4?
In original PHP:
header('Location: http://www.example.com/');
I don't want to do with return Redirect::route('loginpage');. Because it does not work well with ajax. So, how can I do the PHP redirect with Laravel?
Thanks.
If you're wanting to get the url and refresh the page with an ajax function you could simply return the laravel route url and refresh the page with javascript:
// Use the route helper function
return route('loginpage');
// Javascript page redirect
var url = 'Your ajax response here';
window.location.href = url;
return Redirect::to('loginpage')
Is the most common way. Why doesn't that work well with ajax?
When using AJAX, your request isn't handled the way you're expecting it to be. This isn't a laravel-specific issue, as your header() location example would not work either.
If you want to make an ajax request that redirects the user, you'll need to have the response from ajax return something that you can key off of in javascript that executes a window.location change based on the results.
i had some issues with understanding how to get javascript (client) variables transferred so they were acessible from php (serverside) as session : get an iframe's "src" value in PHP?
Now im in a situation where i use firebug to try to debug whats going on, but it just doesnt make sense :
i have this function to update an iframe and i want to pass on the page that that iframe is displaying :
function frameclick(pageurl)
{
$.post("session_write.php?",
{
frameurl : pageurl
}
$("#iFrame1").attr('src', pageurl);
console.log ('<?php echo "logout:".$langpath.$_SESSION['frameurl'];?>');
}
pageurl is ex. "/lang/en/new.htm" - and if i inspect it with firebug i also can see it says that it passes it correctly ( also with conversion of /).
my script serverside that its posted to is like this :
#session_write.php
<?php
session_start();
print_r($_GET['frameurl']);
if (isset($_GET['frameurl']))
{
$_SESSION['frameurl'] = $_GET['frameurl'];
print_r($_SESSION);
}
?>
Posting to that php script on the server will fail via the javascropt - $_SESSION['frameurl'] will be '', but if i ex. do it manually like this :
(http):
//localhost/phpmenu/session_write.php?frameurl=lang%2Fen%2Fnew.htm
then it will be correctly set in the $_SESSION["frameurl"] variable.
I simply cannot understand whats different between doing the javascript post and doing it manually in the browser and why its causing me this problem ?
anyone with an idea ? thanks
You are using .post, which executes a POST request, but when you type in the URL in the address bar, that is a GET request.
$_GET retrieves any params passed through GET, while $_POST retrieves any params passed through POST. So if you use .post with Javascript but try to retrieve with $_GET in PHP, it wouldn't work.
When you POST variables to a PHP file, $_GET is not set. Use $_POST['frameurl'] instead. Also, it looks like you're missing a close paren in frameclick to end the post call.
You are passing data via a
POST request and retrieving for all the GET requests. Use $_POST instead. You may also be interested in $_REQUEST
For example,
I have an ajax request from the page http://www.abc.com/xyz/1 to the function controlling ajax.
Is there any way except sending it through the parameters so that I could know that in the function I can fetch http://www.abc.com/xyz/1 and realise that the ajax request came from here?
If the server wants to know from which page, or the server's location, then you can use this in the end, if PHP:
die($_SERVER["HTTP_HOST"] . $_SERVER["HTTP_REFERER"]);
you can get request url by using below php code:
$request_url=( $_SERVER["HTTP_REFERER"]);
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.