Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling dynamic URLs in PHP5

Status
Not open for further replies.

omniousm

Technical User
Apr 14, 2011
11
GB
I'm having a propblem with an old(ish) script I use for Ad Serving on a small site. It all happened when the server was upgraded to PHP5.

I used PHP includes to call common features of the site - headers, footers etc. and the script used PHP includes to call ads from the relevant category.

After the upgrade my site broke, so I started using the include document root instead of my usual PHP includes: php include _server 'document_root' . 'inc/header.php';

That works fine, although the line that calls the script seems to not work.

I use EditPlus for editing code and the equals (=) sign within the dynamic URL causes the colour-coding to go pink - as in 'something isn't right here'. I assume that's what's causing the problem.

Here's the code:
Code:
<div class="featured">
  <h2><?php include $_SERVER['DOCUMENT_ROOT'] . '/inc/featured-title.php'; ?></h2>
  <ul>
    <?php include $_SERVER['DOCUMENT_ROOT'] . '/manager/display/?cat=1&count=2'; ?>
    <br /><br />
    <li><?php include $_SERVER['DOCUMENT_ROOT'] . '/inc/want-to-be-listed.php'; ?></li>
  </ul>
</div>

The error code I'm getting is:
Warning: include(/home/ukmusicp/public_html/manager/display/?cat=1&count=2) [function.include]: failed to open stream: No such file or directory in /home/ukmusicp/public_html/index.html on line 80

Code:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/manager/display/?cat=1&count=2'; ?>
is line 80 of index.html in the public root.

Any ideas on how I can fix this?

Many thanks!
Ashley
 
you are muddling up protocols.

if you include() a uri with no protocol then php assumes you want a file:// protocol.

however this line
Code:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/manager/display/?cat=1&count=2'; ?>
looks like you are trying to include() an http:// url so you would need to replace $_SERVER['DOCUMENT_ROOT'] with an appropriate qualifier for the protocol, domain etc.
 
Thanks for the prompt response. Can you elaborate on your post? This form of include is new to me.

The script is hosted locally, so I'm unsure as to why the above doesn't work? The path to the script is at the exact location as the above include would suggest - ie mydomain.com/manager/display/... etc.

You're right, I am trying to include() a http file. How would I go about writing that?

Thanks!
 
if you wish to include an http file (i.e. the OUTPUT from the webserver of a visit to that page) then you would do this

Code:
<?php include '[URL unfurl="true"]http://www.domain.name.com/pathToPage/manager/display/?cat=1&count=2';[/URL] ?>

feel free to read up in the manual
 
Ah, that's exactly what I was doing up until the php5 server upgrade. This seemed to stop working after the upgrade so I began using the php5-compliant alternative.

Using your suggested include results in the following error:
Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/ACCOUNT/public_html/index.html on line 12

Line 12 is where the include command is used, using the full URL.
 
Just so it's clear, that was from another line I'd changed to the include() method you'd mentioned - just to avoid any confusion :)
 
You need to change your php.ini to allow URL opening. Don't forget to restart the server after the change.
 
Thanks. I'd already considered that, but isn't that avoiding the whole PHP5 compliance issue as well as the question/what I'm trying to get at?

For example, should I need to move to a server that doesn't allow you to edit your own php.ini I'd be stuffed.

Is there actually a way to call a dynamic URL while being PHP5 compliant?

Would love your thoughts, as much as anyone else's.
Ashley
 
Am not sure what you mean.
There are alternatives to including a remote file. And it really makes little sense to include the output from a script from the same webserver as you're actually serving the content.
 
Apologies if I'm confusing. This isn't my strong point lol. It's a script that handles ads for the site, delivering specific ads based on the category being viewed. So, in that sense, I think it makes perfect sense.

I'll have a play around with the php.ini for now for a quick fix but I'd ideally want an option that doesn't require any modifications of the php.ini file so it is easily transferrable between servers with different levels of 'freedom'.

I believe there are differences in the php.ini file between PHP4 and PHP5 so maybe it's just a value that has been reset in the transition. Being as I hadn't set any values in the php.ini file previously, it used the default settings.

Here's hoping that's the case!

In the mean time, any further response would be appreciated.
 
I may be wrong, but as I understand this, this:
Code:
$_SERVER['DOCUMENT_ROOT'] . '/manager/display/?cat=1&count=2';

This cannot work if you call it as a file, because it relies on the server's mod_rewrite engine to change from a pageless url to an actual file call to pass the parameters to.

include requires that there be an actual file to open. However you call stops at the "display" folder and then starts passing parameters. since there is no physical file include errors out.

I'm not sure if the re-written url will get passed to the include call though even using the HTTP protocol, so perhaps trying to include the direct file would be better.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks Phil. I see your point.

Before the upgrade this worked fine, even tho no actual file is called. That is, using
Code:
<? include('[URL unfurl="true"]http://www.domain.com/manager/display/?cat=1&count=2');[/URL] ?>
I guess it 'assumed' to use index.php at - the only file within that folder.

I've updated this to point to , thinking php5 might be a bit more pedantic, but still no luck.

I've even set both allow_url_fopen = On and allow_url_include = On in php.ini to no avail.

I'm on a shared server so don't have access to reboot this. Are they generally rebooted at regular intervals or will I need to request a reboot? I remember updating the php.ini on another site and changes were pretty much instant

I also think I neglected to say I'm running cPanel.

Further ideas much appreciated.
 
According to the color-coding of my code in EditPlus, it seems to be the equals sign in the dynamic URL that is causing the problems. Without these, eg. with a url to a static page, the color-coding appears fine.

Maybe there is an alternative to get around this issue? Another symbol? Escaping? Another form of include - echo?

Code:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/manager/display/?cat=1&count=2'; ?>

Any ideas welcome.
 
Are you sure the index.php inside display is the file that actually runs whatever script you want included? It may just be a place holder while the mod-rewrite sedns the request somewhere else.

Also do you get any errors or does it just silently fail to include anything?

As as a test why don't you put a script inside display/ that echo's something so you know its including and try to include that directly.

You could also use the ini_get() function to make sure the directive is actually set.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
I do get errors - 2 of them. The code:
Code:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/manager/display/?cat=1&count=2'; ?>

... results in the following errors to be output into the document.

Warning: include(/home/ACCOUNT/public_html/manager/display/?cat=1&count=2) [function.include]: failed to open stream: No such file or directory in /home/ACCOUNT/public_html/index.html on line 81

Warning: include() [function.include]: Failed opening '/home/ACCOUNT/public_html/manager/display/?cat=1&count=2' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/ACCOUNT/public_html/index.html on line 81

FYI the CAT variable determines which category of ads to pull, while COUNT determines how many ads to display, in this case 2 - hence the 2 errors.

index.php is the only file within the /display/ category and cannot see any .htaccess files that would suggest the server should pull from elsewhere.

So confused :( lol
 
After further research I found the following, which has helped a little:
Code:
<?php
  $includeFile = file_get_contents("[URL unfurl="true"]http://www.domain.com/manager/display/?cat=1&count=2");[/URL]
  echo $includeFile;
?>

It is including the ads and displaying 2 of them (count=2), but no actual ad contents are being displayed. I'm assuming its further issues with similar includes.

Rather than dig any deeper into a non-upgradable script, I've decided to go with phpAdsNew/OpenAds and give that a bash, as it seems to handle everything much better - or at least it did when I used it a few years back before it became OpenX.

If not, I'll be going back to this.

Thanks for the help guys!
 
that is no different to writing

Code:
include '[URL unfurl="true"]http://www.domain.com/manager/display/?cat=1&count=2';[/URL]
 
Haha thats what I thought but it seems to work on the server. Strange php.ini config I guess, but I'm sure the settings have been set for a reason - most probably security, rather than just to be awkward.

I'm checking out OpenX as this seems like a much more comprehensive approach to what I'm after and can server numerous sites.

I can't believe such a simple thing can cause so much trouble and headache after a PHP upgrade :/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top