Traxus Posted July 14, 2010 Share Posted July 14, 2010 I can't seem to use variables in a php script when they are in a php file included from another domain. I have allow_url_include on, and have successfully included php files from the other domain, spare this detail. Here's the situation, I've got a php script that will build a contact form based on feilds defined in an xml file and variables set in a config.php file. The config.php file has a variable that defines the path to the xml file, as well as the path to the form builder php file, and other things like the subject of the email that will be sent upon filling out the form, and the recipients email address. At the moment, I've got this setup running on about 15 different domains. Every time that I need to update the form builder, I have to make the changes and then re upload the file to each domain, which is becoming a pain in my behind. I decided to try and call this file from one of my personal domains, that way any new update would apply to each domain that called it. However, I wanted the unique form info (xml and config.php file) to remain on the clients domain, as that feels more sensible to me. When I include a page with the following code, from mypersonaldomain.com the everything works: $xml_path = "http://www.clientdomain01.com/form_feilds.xml"; function form_generator() { //lotsa code } form_generator(); But when I do this on clientdomain01.com, the second file won't read the variables from the first: require_once ('site/relative/path/to/config.php'); //this is the config page that points to the xml require_once ('http://www.mypersonaldomain.com/form_builder.php'); //this is the php page that reads the config.php and xml files (for reference, form_builder.php looks something like): function form_generator() { //lotsa code that reads config.php and form_feilds.xml } form_generator(); I've done some expirimenting, by including a variables.php page and then trying to echo those variables, and the echo wont display the vars from the other domain, so I'm sure this is the bottleneck. Variables are global where they need to be... Ive tried global $var1, $var2; as well as $GLOBAL['var1']... Googling hints that this needs to be done through $_POST and fsockopen(); but I haven't found any concrete examples... Is this a security feature, or just a php.ini setting? It seems that, if i could include a variables.php page from the same domain and utilize those vars, I could also do the same with a page on a different domain? I'm aware that there are third party solutions for hosted contact forms etc... But I would like to learn this cross domain for future application. Thanks for your time... [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/207742-calling-variables-from-php-files-included-from-another-domain/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 When you use a URL in an include, you only get the output from the remote file because it is parsed on the remote server when is it services the request for that URL. I.E. The same as if you browses to the remote file. To do what you want would require that the remote file you are requesting output (echo/print) the raw value you are expecting and your code would need to assign the value to a variable or you would need to output raw php code (with the opening and closing php tags as part of the output.) Quote Link to comment https://forums.phpfreaks.com/topic/207742-calling-variables-from-php-files-included-from-another-domain/#findComment-1085990 Share on other sites More sharing options...
Traxus Posted July 14, 2010 Author Share Posted July 14, 2010 When you use a URL in an include, you only get the output from the remote file because it is parsed on the remote server when is services the request for that URL. I.E. The same as if you browses to the remote file. I had a feeling it was something like this, but couldn't wrap my head around it. Would fsockopen(); or any of the file reading functions avert this? If so, would it be hopelessly inefficient regarding bandwidth and processing power? To do what you want would require that the remote file you are requesting output (echo/print) the raw value you are expecting and your code would need to assign the value to a variable or you would need to output raw php code (with the opening and closing php tags as part of the output.) That's intresting, I didn't know you could echo entire blocks of php code... in other words: $crossDomainVar = "value"; echo "<?php \$crossDomainVar='".$crossDomainVar."' ?>"; Quote Link to comment https://forums.phpfreaks.com/topic/207742-calling-variables-from-php-files-included-from-another-domain/#findComment-1085995 Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2010 Share Posted July 14, 2010 You can just use file_get_contents() to read the URL, but the remote code would need to output what you want in the format that you want. Yes you can echo raw php code, as long as it's valid code. You can also put php code into a file with an extension, such as .txt, that is not automatically parsed as php code. Quote Link to comment https://forums.phpfreaks.com/topic/207742-calling-variables-from-php-files-included-from-another-domain/#findComment-1085996 Share on other sites More sharing options...
Traxus Posted July 16, 2010 Author Share Posted July 16, 2010 You can just use file_get_contents() to read the URL, but the remote code would need to output what you want in the format that you want. Yes you can echo raw php code, as long as it's valid code. Thanks for your help. I was able to pass the variables from the local config.php file to the remote form_builder.php file via this method, constructing the input form correctly: $crossDomainVar = "value"; echo "<?php \$crossDomainVar='".$crossDomainVar."' ?>"; However, I'm kicking myself for not realizing that the remote results_builder.php relies on $_POST variables that come from the local site. You can also put php code into a file with an extension, such as .txt, that is not automatically parsed as php code. Can you (or anyone?) point me to some examples or documentation on that? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/207742-calling-variables-from-php-files-included-from-another-domain/#findComment-1086819 Share on other sites More sharing options...
Traxus Posted July 16, 2010 Author Share Posted July 16, 2010 Disregard that last post... You gave all the information I needed, thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/207742-calling-variables-from-php-files-included-from-another-domain/#findComment-1086873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.