Passing URL as Argument to PHP file - php

I'm writing a php script that will fetch data from a given URL and then run a lot of calculations based on that data, and then output it for the user.
The web page in question is a page with an embedded iframe. The iframe contains javascript code that contains the data I need, and unfortunately, the iframe is not hosted on the same domain as the web page. So what I'm trying to do is extract the URL to the iframe from the web page (I can at least do that without hitting cross-domain restrictions), and then pass the URL to the php file, and it will load that URL, and find the information.
This raises a slight problem of the URL having lots of arguments it in already. It's shaped in the form of www.somesite.com/iframe.php?userstring=asfjkl&arg1=654&arg2=132%2C9848%2C698
The problem is that the URL already has arguments, and this will not work well for my php file, which will confuse those arguments being arguments, instead of being part of the URL when I redirect the user to my site's URL of mysite.com/test.php?URL=(string shown above). So now, I'm thinking about a POST request, but sending a POST request will not redirect the users unless I actually have a form. So my question is, if that is a viable idea, to send the URL by creating an invisible form and setting the data to the URL, then submitting it, and if cross domain submission for form is allowed. (I think it is). Any other suggestions?

Simply urlencode() the URL argument. Then everything works fine.
urlencode('http://www.example.com/blah?x=y&a=123&something=else');
// Returns http%3A%2F%2Fwww.example.com%2Fblah%3Fx%3Dy%26a%3D123%26something%3Delse

Related

relation between the "?" and GET method?

I was wondering what might be the relation between the question mark, the variable after the question mark ('show' in this case) and the way it is being passed to the GET method in PHP. Also, why don't we use POST instead of GET?
<?php
if (isset($_GET['show'])
echo $_GET['show'];
?>
<input type="submit" onClick="window.location='index.php?
show=include.inc.php'">
Oh, and the file I am working on is index.php and upon a click, it shows the content of include.inc.php.
Please help. Sorry for any stupid questions.
The question mark denotes that there is a variable to follow!
The way that the $_GET function works is by passing the variables through the URL itself! In this case, its like putting up a sign that says "Hey, show is equal to include.inc.php".
$_POST does the same, but more discreetly. Instead of passing it through the URL, it creates a little package that travels to the receiver, similar to the postal system.
I was wondering what might be the relation between the question mark, the variable after the question mark ('show' in this case) and the way it is being passed to the GET method in PHP.
The question mark indicates the start of a query string in a URL.
The URL is the only convenient place to put data in an HTTP GET request because there is no request body. An HTML form will, by default, when submitted, trigger a GET request and encode the content of the form in the query string.
PHP puts data from the query string in $_GET. It does this no matter what the HTTP request method actually was. It is a poorly named variable name ($_QUERY would be better).
why don't we use POST instead of GET?
You can't bookmark or link to a POST request.
Refreshing the page after making a POST request will prompt the browser to ask the user if they really want to resubmit the data.
In short: POST is designed for making requests which change data on the server (which don't generally make sense to repeat). GET is designed for making requests which only get data from the server (and are thus repeatable).
The code after the ? question mark is what is sent to the server in the HTTP request. (in the format: http://...URL...?key_1=value_1&key_2=value_2)
The onClick="..." code is javascript code executed when the user clicks that button in the browser.
window.location = ... is javascript code to force the browser to change the URL page to whatever it is assigned.
You can use POST instead of GET the data being sent to the server won't be as easily seen by the user in the browser, it won't become part of the URL. E.g. user can bookmark URLs with GET but not with POST data.

How to forward (not redirect) requests in PHP?

I am working on a simple PHP site that involves needing to be able to forward a request made by the user to another page (note that I said forward, and not redirect). I am aware of how to redirect by manipulating the header variable, but I do not wish to do this, as explained below.
I am trying to write a very simple MVC-patterned mailing list app in PHP, drawing from my knowledge of the same in JSP. One of the things that I appreciated about JSP was that you could both forward or redirect a request. For my purposes, I need forward as I wish to keep the request parameters (whereas redirect will drop them).
Here is a description of what I wish to accomplish:
Retrieve input from a form (ie. /add.php)
Process the input in the page called by the form's action (ie. /process.php) and add a success message to the request object
Forward to another page (ie. /display.php) to display the success message in the request object
The only way I am aware of passing the request message to display is to add it to the request object and then access it from the forwarded page. However, the only way I have had success in transitioning to another page is through using the header method, which drops the request object (from what I can tell). I want to find a way to forward the request (object) to the new page, so that I can access the request variables from the new page.
Is there actually anyway to do this in PHP? Java's getRequestDispatcher.forward() is so nice, but I can't find an equivalent through searching. I've tried several similar questions, including the following, but I've never actually found one where both the question and the answer were what I wanted. Most of the answers seem to have something to do with cURL, but I don't want to actually retrieve a file, but simply forward a request in order to access the request object from another page.
Does PHP have an equivalent of Java's getRequestDispatcher.forward()?
Let me know if I should include anything else?
I believe you can do this with include. Before submitting the form just use, as inclusion, in main page:
include ("add.php"); - where the input forms are
after processing the information, include the display.php in the same way; using this, display.php will use same parameters from header, because is included in the same main page.
briefly: add.php, process.php and display.php will be modules for the mother page, but loaded in different state of form processing.
Hope it helps!
use curl with different method get,post. it will sent a request and also get back the response.
The most common method I see of passing messages to the end user from page to page is called session flashing.
This is when you store a variable temporarily in the session until it is read.
Assuming you already have sessions in use:
On process.php:
$_SESSION['message'] = 'Your data has been saved!';
On display.php:
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
You could also store the entire Request object in the session.
So if I am aware, PHP provides just basic set of tools in this case. And there is nothing like "forward" in HTTP originally. It is just frameworks' abstraction/idea. There are two ways to achieve that: copying all params from request and doing new real HTTP request (with redirect) or internal forward: so framework would create fake request and call another controller and action without issuing a new physical HTTP request.

PHP/JS URL inside of GET request

This question is directly related to this other question found on StackOverflow.
I have the same problem - but I'm not sure I can separate the protocol prefix as another variable, as suggested in the above question. I'm using the GET action, sent to another page where the data is used (e.g. form on index.php submits GET to search.php).
On the index page, a user can type data, including a URL, to be submitted (e.g. text field and submit button). Due to the nature of URLs, I expect to have some people who copy and paste - and thus people are likely going to include the http:// sometimes, whether I want them to or not.
If http: is included in the GET request (e.g. search.php?q=http://google.com), then I receive a 403 Forbidden error on search.php - which is where I'm running into issues.
Outside of JS, is there a way to either remove or separate the protocol prefix from a user input, if it exists, before the request is sent to search.php? E.G. After user clicks submit but before data is sent to another page?
Thanks in advance for any answers or advice you can give!
EDIT: I know I can use urlencode to encode URLs - but can that be done before the data/GET request is actually sent?
Outside of javascript? No.
However, when the data is sent to search.php, you can test for the http:// and remove it you don't want it included.

PHP Ajax return source

I've got the following problem, I send some value from jQuery to PHP via AJAX. My PHP script receives those value, and I would like that this script prints values in iframe in my www site. But instead, the script response contains all its source code to AJAX as response to the alert message. Does anybody know how can I stop returning code to AJAX and execute it by returning the values to be used in my iframe?
EDIT:
further details: the problem is that my application has got two iframes from different servers, and I need to send values from one iframe to another. Because of cross domain restrictions, I cant do this via JS directly, so I figured out to send values via AJAX from one iframe to PHP on first server and PHP from first server to PHP on another server which is in iframe on my main page, and then show the data. so I must use AJAX to send it.
You can't. PHP doesn't know about the iframe.
You are, presumably, using XHR to make the request, therefore XHR will receive the response.
If you want to load a document into an iframe, then load a document into the iframe (e.g. by setting its src to the URI for the document), don't play around with Ajax.

Get url of the page in php

I am working on "Email this page" Popup page. I want to send url of base page as an email, but it should be a popup window.
I have used HTTP_REFERER, it is working fine on Firefox, but not working on Internet Explorer.
I am getting the url of current page but I want that url in new popup window page.
Is there any alternative than HTTP_REFERER.
On the page you wish to grab the URL of, you can use $_SERVER['REQUEST_URI'] to get the requested URI (except the scheme & hostname; in other words, you get the path and query string). Pass this to your other page either using a query string or sessions. The former is preferable, as the latter isn't RESTful. There may be times when it's OK to break REST's rule against server side state, but this probably isn't it.
There is no way unless you store it or send it yourself. This page has one example of how to do it, but only really if you set it beforehand. If the site is your own then you should be ok. If not then you will struggle.
That happens because the HTTP_REFERER is sent by the client browser, which means that it's value can be totally manipulated or can even be null. This means that this variable isn't very reliable. But if the site is yours, there are other solutions.
You can send the url or any other identification like an ID by QueryStrings. So you'll have the link URL like this the_send_page_name.php?ref=index.php
Be aware that this method only works if you're opening the Pop-up in a site that's yours.

Categories