php: include with URL parameter - php

I'm trying to include db.php and pass a variable to it.
This is what I have:
include "https://imprss.herokuapp.com/db.php?id=6626068";
Looks perfect to me but it simply won't work.

To use a protocol such as https in your include directive, you should make sure that allow_url_fopen is enabled in your php.ini file.

https://imprss.herokuapp.com/db.php?id=6626068 is not returning PHP code, it outputs HTML code to be interpreted by the browser, if you are interested in getting the HTML code of that page then dont use include() instead you should use cURL functions or the fopen url handlers .

I would use a relative path, but only thing you are missing are braces around the quotes which define url.

It is not possible to include a php file on another server, because this would be a security vulnerability. You can, however, use file_get_contents to get the output of a php file.

It's asked before and answered. just go to link bottom:
How to pass parameters with include?

Related

file_get_contents() in php - need to pass static parameters and it's not working

I have a simple file I have created:
<?php echo file_get_contents("http://occupancyadvantage.com/shortquiz.aspx?sid=234"); ?>
I can only get the file to render properly when I remove the parameters (well, when you say properly, I doubt you would consider that (http://occupancyadvantage.com/processfree.php -- the %# page.... is showing up) But I could care less about that.
I'm just trying to get a php file to load this aspx file with the ?sid=243 parameter on the end of it.
I don't have access to curl - I have a few weeks without the tech guy here and I'm not allowed to make that kind of an install w/o him... I do have access to the ini file here is what it does have. If something isn't on, I don't know what it is: http://occupancyadvantage.com/phpinfo.php
I've been killing myself with this and I just can't seem to figure it out. Besides punching babies I figured this place was the best option.
If you could please, try to spell out your responses the best you can, as being vague will probably just end up with me researching and asking more questions. I appreciate it.
This note from PHP's documentation on file_get_contents() might help:
If you're opening a URI with special characters, such as spaces, you
need to encode the URI with urlencode().
So that would be:
<?php echo file_get_contents(urlencode("shortquiz.aspx?sid=234")); ?>
If that doesn't work, maybe use the full URL? As in:
<?php echo file_get_contents(urlencode("http://your-site-here/shortquiz.aspx?sid=234")); ?>
You can't file_get_contents with parameters if it's a local file. Just like you can't actually open the file with the parameters in a text file, it doesn't make sense to F_g_c.
You can, if the aspx is http-able, use f_g_c(http:/...).
Otherwise, you'll have to use wget (there are nastier ways to do it such as exec, but don't use them unless you don't need security at all).
You are trying to open the file named shortquiz.aspx?sid=234 in the current working directory. I'm guessing there is no such file in the current working directory.
If you want to fetch the url that ends with shortquiz.aspx?sid=234, you can try this:
file_get_contents('http://example.org/shortquiz.aspx?sid=234');
Of course, replace "example.org" with the full host and path for this URL.
I ended up using an iFrame to do this. I don't know what I was thinking. Thank you for all of your time.

Force php to look at Document Root

So I was messing around with the twitter api, and I want to include this file in the footer for every page. The footer is loaded via phps require_once function. The problem is I can't use a full url because of url file acsess being turned off for obvious reasons. So I tried to use
require_once $_SERVER['DOCUMENT_ROOT']('/lib/twitter/base.php');
But that failed. What am I doing wrong or how can I do this better?
Try this:
require_once($_SERVER['DOCUMENT_ROOT'].'/lib/twitter/base.php');

get URL in php IN INCLUDE FILE

i have some problem i try to get the uri in php.
I'm using:
$_SERVER['REQUEST_URI']
It works just fine if i do it in the index.php, but, i NEED to get the url in a include file, but, when i do it, it takes the FILE adress, i mean, it shows something like this
adress bar: www.webpage.com/index.php
$_SERVER['REQUEST_URI'] output: webpage/includefile.php
I am explaining myself here? Thanks!
How are you including the file? If it's being included via an HTTP reference then it's actually being served as a page and the functionality you are seeing is correct. If the include path is a local file, you shouldn't be seeing this behaviour
Found this whilst trying to solve the same issue.
My solution that worked is to use $_SERVER['HTTP_REFERER']
This worked well in that it also included the parameters (e.g. ?this=that&foo=bar)
Maybe somewhere in your code (or in another include file) the value is overwritten.

Using a query-string in require_once in PHP

On one of my pages I have a require_once('../path/to/url/page.php'); which works with no problems. The moment I add a query string require_once('../path/to/url/page.php?var=test'); it won't include the file anymore. It's just blank. Anyone have any ideas of why? Can you not use a query-string in a require?
Thanks,
Ryan
By using require_once('../path/to/url/page.php?var=test');, php will not make a new request to page.php, it will actually search for the file named page.php?var=test and include it, because in unix, you are allowed to have such a filename. If you want to pass a variable to that script, just define it: $var="test" and it will be available for use in that script.
require loads a File (from a file path) to include. It does not request that file through apache (or other webserver), therefore you cannot pass query strings in this way.
If you need to pass data into the file, you can simply define a standard php variable.
Example
<?php $a_variable = "data"; require_once('../path/to/url/page.php'); ?>
Note, the variable must be set before the include/require is called, otherwise it won't be available.
All answes true. But most importantly: since $_GET is a global, it's present' in all included files as well, so there's absolutely no use in passing those parameters with the include.
require only accepts paths it would be pointless to add any request since it doesn't make any
it simple includes the required code into the current one

Storing the HTML output from a local PHP file into a string using file_get_contents

There is a header.php file and it contains some php codes that return HTML.
I know I can use require, include to echo the results, but what I want to do is to store its processed output string into a variable.
In a page, I used:
$headerHTML=file_get_contents('header.php');
Then I got the PHP code output rather than the processed HTML output.
I know adding http:// would help.
But I prefer to keep using relative path, how can I tell the function to treat the php file correctly?
Note: I would like to continue to use this statement file_get_contents rather than using ob_start() if possible.
I'd rather use require() wrapped inside ob_start() and ob_get_clean(). I am sure there is nothing wrong with this approach.
Don't use eval() - it's evil!
Use the relative local path an automatically map it to a absolute URL.
If URL wrappers are enabled and you want the output of header.php (and you don't want to keep session state) you could use $headerHTML=file_get_contents('http://yourdomain.tld/path/to/header.php');, though why you would want to do such a thing eludes me. Are you sure you're not trying to do something that could easily be solved by using templates and caching?
You can check http://in2.php.net/manual/en/function.eval.php#56641, hope it helps.

Categories