Send a Variable to an IFrame's Parent in PHP? - php

How can I transfer an array from an IFrame running a php script, to the parent PHP of the IFrame?
I have an Array of strings in PHP that need to be transferred to the main window so that when the iframe changes pages, the array is still stored.
I could use a cookie for this, though I'm not sure that would be the best idea.
Thanks.

you can't do that in php. iframe is like a new browser window, so they are separate requests. separate requests can not transfer data between each other in a server side language.
if you give some detail as to what you're trying to accomplish, there may be another way around the issue that someone can suggest.

Like Tim Hoolihan said, PHP is a server side language that is only responsible for generating the HTML before it is sent to the browser. Meaning once the page shows up in your browser window, PHP has done it's part.
However, with that said, if you control both the "parent" page and the page being iframed, you can json_encode the array in the page being iframed and set it to Javascript variable, then on load pass it to a Javascript function on the parent page (assuming not violating any browser/domain sandbox constraints). At that point you can do whatever you want with it.
Take a look at jQuery for your core Javascript/Ajax needs.

if you control the iframe, you can save the array in a session variable and make the parent do an asynchronous call to retrieve the array from session.
however Jordan S. Jones solution with only javascript works as well

Related

Get contents of DOM via PHP

I need to get the contents of a website through PHP, however, the content is only available when JavaScript is enabled. The workaround that I am using now is making an applescript to open the website in Safari, and selecting all of the page content, copying it to the clipboard, and pasting it.
That will be really hard to achieve I guess. If you observe the JS on that page that is responsible for getting the content ready, you may discover its just another AJAX call that you may be able to call directly from your PHP script.
best possible solution: ask the website owner for api/export access ;)
If that is not possible, you can only pray that you can analyze the requests that are initialized via JavaScript and imitate them.
(possible tools: firefox with firebug or tamper data plugin).
Warning the owner of the website might not like this approach, in fact, it may be disallowed to scrape the data automatically
What do you mean by:
the content is only available when JavaScript is enabled
Does the page pull data from somewhere via JS? Would it be easier to analyse where the data is coming from and access that place directly from PHP?

PHP $_SESSION not available until the page is reloaded. Why?

I'm creating a graph that does some database queries then builds the graph using GD functions then also puts the info from the database into an array that is stored in $_SESSION['my_data']. This graph is displayed on the web page using img tags.
<img src="my_graph.php?time=$time">
<?
print_r($_SESSION['my_data']);
?>
The problem is when I print_r() the array from the session variable it doesn't print the current graphs data; it prints the data that was in the graph last time the page was loaded. It's acting like a cookie.
I have tried putting session_write_close() immediately after I store the array in the session because I thought $_SESSION might have been locked until my_graph.php finished loading the image. That did not work.
I also tried putting sleep(10) before I print the array and that did not work.
Why this would happen?
I suppose, when your web server executes PHP code, which draws our page, it already has $_SESSION array initialized. And this array is not updated in current script runtime. When browser finds image tag, it makes another request to web server, which executes image generation script, which updates $_SESSION array in another runtime.
You may:
either make all calculations in your web page generation code
or reload page after image generation script completes calculations and sets all necessary data in $_SESSION array
If you are setting $_SESSION['my_data'] in mygraph.php, you will never see your $_SESSIONchange until your browser requests mygraph.php. This will never occur until the output is flushed to the browser (which will be AFTER you've already print_r() the $_SESSION).
You may be able to try flush() and hope the browser requests the image before you are done executing, I've never tried that (not sure if it will work). Though, sometimes you have to pad output with whitespace until it is about 2k (if I'm not mistaken). I wouldn't recommend this, though.
Another solution would be to request the page you have your code in above in the src. So if your code above is in test.php you could put <img src="test.php?img=true&time=$time">. Then if you get $_GET['img'], display an image, otherwise execute some code. Does that make sense?
Sequence of events:
Browser requests the main web page.
PHP runs the main page and sends the contents to the browser.
Browser receives the page.
Browser looks at the page and sends requests to load the graphics, etc.
PHP runs graph.php and sends the graphic to the browser.
Browser inserts the graphic into the page.
The important thing to note is that the whole of point 2 occurs before any of point 5 happens. This means that anything that graph.php does with $_SESSION will not be visible to the code in the main page, leading to the effect you're seeing.
This is the nature of a web page: the graphic files are separate from the main PHP program.
You cannot get around this using a separate graphic file the way you're doing it.
There is one way I can think of achieving it, but it would be a complete rewrite (it's up to you to decide whether it's worth it!)
It is possible to create a graph using Javascript. If you generate the javascript code to do this in the main page, you could then set $_SESSION during the graph generation code, and it would be available later in the program.
If you do decide to do this, you should look into the gRaphael library to help you out.
Hope that helps.

PHP and JQuery Question

Is there a way I can pass a PHP variable from a PHP page to a JQuery page and back to a PHP page?
Maybe I'm wrong but I believe you may be confusing things.
Server Generates page using PHP.
Generated page is composed of HTML, JAVASCRIPT whatever...
Client receives page
Received page is interpreted (JavaScript code is run)
In the end what you are asking for is possible but, by the way you put the question, I though that the above should be clarified.
How to do it?
Say you want to pass id=123 from server to client and then back.
generate page with a tag, say <span id="js-val">123</span>
have client read the contents of id="js-val
client can then resend the 123 using POST or GET that really depends on what you want.
Hope it helps to clear things up.

javascript php array

i have a js array like this:
var myArray = [];
myArray[1] = 'test';
myArray[2] = 'test';
-i want to hide it from users to see it. how can i store just the array in a php script and call it?
right now i have the .js separate from my form. and i just call it. but anyone can easily view source and visit the url using the .js name
-another question i have is to hide a url values from the user. i have something like this:
www.test.ca/people.php?id=12
i want to hide the values. thanks
For the JS code, if the browser has to execute it, then the user can see it. Not much you can do.
If you want to carry values between pages and you don't want them to be seen, don't use a query string -- use PHP sessions instead.
All Javascript code is viewable from the client. There really is no way around this.
Even an AJAX call can be viewed via a good browser plugin.
Javascript is a client-side executed script, so you won't ever be able to hide it.
You can encrypt it, you can make it difficult to view it, but that's pretty useless.
Just put it in your sources, or if you want to hide it a little further, get the array with an AJAX call, and make the call show nothing when it's not called with AJAX (the array can still be revealed with developper browser plugins, or with being hacked adding extra headers.
Here's the PHP condition code : if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])
Don't try to make it harder then that, it will be a waste of time.
Think that the browser is a transparent box. Everything you want to hide, needs to sit on the server.
If you want to send data across multiple pages, you have two options -
Use PHP Sessions
Use hidden fields
I would recommend the second option, because PHP sessions have the same problem as using a global variable, i.e., you can't use the same key in the whole applications, it is harder to maintain a session etc.
You can't hide a JS code from the user, because the browser will certainly execute it.

Page changed in JQUERY

I am trying to found out how to see if a php file has changed and then show a div with saying Page changed in JQUERY
You'd better do that in PHP using filemtime, no need for JQuery here.
You only need jQuery for this task if you're trying to detect the page change without waiting for the user to request a new page. If not, do as the other responder suggests and use PHP.
But if you need to do it without a page reload, use one of the $.ajax() methods in jQuery in combination with a JavaScript timer. You'll have to poll the server periodically (thus the timer) to ask if the page has been altered.
You would also need to set up something on the server that can tell your page about changes. Perhaps a very simple service that provides the timestamp of the last edit in JSON format. Use $.ajax() to poll for the timestamp, then compare it with the last edit the page knows about. If the timestamp from JSON is more recent, display your div.
Javascript cannot access the server, you will have to use some sort of server side technology. Like PHP that was suggested by Pekka.
In short, javascript is client side, which means it interacts with the user on their side, while php is server side, meaning it interacts with the server. Checking the file modified date is a server side issue, your client isn't serving the pages (unless you're on freenet)
Or you could output a <meta> tag for when the page was updated with PHP or whatever framework or language you are using. Then create a cookie with your JS and compare the cookie with the meta tags content.
Ugly solution but it would work. I wouldn't want to resort that this however.

Categories