Access global variable in <iframe> - php

I have a page that is included as an iframe in my site. I need to supply the searched keyword to that page for DFP attribute and so I am setting the searched keyword as a global variable and now I need to retrieve the global variable in that iframe. So is it possible, and if so, how?

You can not access global variable in iframe because both will be separate request to server.
Either you need to pass variable as paramater in iframe src or you need to bind session variable in parent page and use that session value in iframe page.

You don't access this global variable in iframe because of limited scope. You can pass this variable in quarystring parameter. As like for example :-
<?php
var $param="test";
echo "<iframe src='test.php?param=".$param."'></iframe>";
?>
I hope it may helps!!

Related

Passing a $_SESSION using <a href> outside php tags

I'm working with PHP and in this instance I want to pass $_SESSION which I've already assigned with my currentUser object. This works fine.
I've used the header function to pass variables between pages such as header('Location: example.php?uID='.$uID) and then using GET to receive this value.
However, instead of using a header to automatically redirect, I want to use a href so that the user has to click on a link, which then initiates the redirect.
The problem is that I use the anchor (<a>) inside the PHP tags and I can't pass variables using it inside the HTML tags.
I'd be very grateful for the solution and happy to elaborate. Thank you!
You can do it that way :
//assuming that the user id is stored in the 'uID' field of $_SESSION
Redirect me !

PHP: Pass a variable in URL via a clicked link

I created a function that requires one parameter to be passed into it but I need that parameter to come from another script. How do I embed the variable into a link then put that variable into the function in another script? I know I need to utilize $_GET, isset($_GET['']) and a href but I just can't put it all together.
$_GET is collection of of query string parameters. If you had a url like: test.php?param1=foo&param2=bar you can access foo by $_GET['param1'] etc..
Use $_REQUEST['something']
Your link must be:
Click Me
Inside test.php
if(#$_REQUEST['something']=="something"){
echo $_REQUEST['something1'];
}
When you click Click Me, test.php would echo "test".

pass variable in the same class from index page to an iframe

I am trying to create a game with PHP. I have an iframe in index page all functions are in same class but I can't access the variables from the functions if I am calling a function in iframe function page. I have tried $_SESSION , but it didn't work,they are empty,help
When using Sessions, did you call the session_start function?
You can pass it with a GET variable in the URL on the iframe src attribute (if that's not a sensitive data or something like that).

Storing redirect URL for later use

I'm trying to store the redirect URL for use a few pages later but I'm having trouble figuring out how to get it from one place to another.
Usually I'd just pass a variable thru the URL, but since my redirect URL contains URL variables itself, this doesn't exactly work.
To give you a better idea of what I'm trying to do, here's the structure.
PAGE 1: User can click a link to add content on PAGE 2
PAGE 2: User enters text. Submitting the form on this page calls "formsubmit.php" where the MySQL data entries are handled. At the end of this I need to redirect the user to PAGE 1 again. The redirect URL needs to exactly match what was originally on PAGE 1
Does anyone have any suggestions on how to go about this?
You should use $_SESSION to store the variable in session memory. As far as specifics go with how to handle this in particular, you should be able to figure it out (store the variable, check if it exists later, if so redirect etc etc) but $_SESSION is going to be much more efficient / less messy than trying to pass things back and forth in query strings.
To declare a session variable you would do something like this:
$_SESSION['redirUrl'] = "http://www.lolthisisaurl.com/lolagain";
And then to reference it you just do
$theUrl = $_SESSION['redirUrl'];
Here is some material to get you started: http://php.net/manual/en/reserved.variables.session.php
I would recommend either using session variables, or storing the redirect url in a hidden form parameter. Session variables are pretty simple; just initialize the session (once, at the top of each page), and then assign variables to the $_SESSION global var:
<?php
session_start();
...
$_SESSION['redirect_url'] = whatever.com;
...
Hidden form parameters work by sending the data from page to page as form data. On the backend, you would add code that would put the URL to be stored in a form variable:
<input type='hidden' name='redirect_url' value='<?php echo $redirect_url; ?>';
On each page, you can take the URL out of the $_POST or $_GET variable (whichever is appropriate) and insert it into a hidden form in the next page.
You can use urlencode and urldecode to pass a string that contains elements that would otherwise break a url in a url query.
I can see two possible solutions :
get the previous page from document.referrer ([edit] find more info on this SO thread : getting last page URL from history object - cross browser?)
store the previous url via a session variable ([edit] MoarCodePlz pointed this out in his answer)
Regards,
Max
You can add this hidden field in to your form:
<input type="hidden" name="referer" value="<?php echo $_SERVER['HTTP_REFERER']; ?>">
Then use header() to redirect to this page:
header('Location: '. $_POST['referer']);

Getting variable in iframe

I'm passing a varaible to a iframe but can't seem to access this variable via the global get variable.
<iframe src="uploadContract.php?clientid={$clientid}" frameborder="0" height="95"></iframe>
in uploadContract.php I try to access the variable like $_GET['clientid'];
Somehow I can't seem to get the value via the Get global
What am I doing wrong?
What I noticed is that this iframe is within another frame, if I display the iframe not within a frame it actually works. How can I get this to work within a frame?
Check in the html that the src is really what you think it is.
Then try var_dump($_GET); from uploadContract.php
I doubt that the problem is in the code snippet that you present here. Are you sure that $clientid has a valid value when you try to use it in the above snippet?

Categories