Forum Moderators: coopster

Message Too Old, No Replies

PHP Include with a parameter doesnt work

PHP Include with a parameter doesnt work

         

dunhill

12:22 pm on Dec 16, 2006 (gmt 0)

10+ Year Member



I have written a number of php pages, they all seem to run very slowly.

I was doing includes this way
<? @include ("http://www.mywebsite.com/advertising/mypage.php");?>

I worked out if I change it to relative it works much much better

<? @include ("../../../../advertising/mypage.php");?>

The question is I can’t seem change it if the php has a parameter for example this script

<?
@include("http://www.mywebsite.com/ advertising/mynewpage.php?p_pc_title=$var_title");
?>

If I change the path to be relative it doesn’t work, it doesn’t run and it doesn’t produce an error.

Can you change it to relative path if you are passing parameters?

Is there any other performance things I can do to speed up my pages?

Thank you

dunhill

12:58 pm on Dec 16, 2006 (gmt 0)

10+ Year Member



Ok I have learn't something whilst RTFMing

The proper way is to use DOCUMENT_ROOT, so now I do this

$MYROOT=$_SERVER['DOCUMENT_ROOT']
<?php @include($MYROOT . "/advertising/mypage.php");?>

This works great, but still doesn't work if I have paramters

I tried this, but it doesn't work
$MYROOT=$_SERVER['DOCUMENT_ROOT']
<?php @include($MYROOT . "/mynewpage.php?p_pc_title=$var_title");?>

eelixduppy

2:47 pm on Dec 16, 2006 (gmt 0)



What are you including from that file?

It looks like it would make sense to declare the GET variable before you include it, and then within the included file check for a GET variable, if one doesn't exist, then check for another declared variable. Something like this:


$p_pc_title = $var_title;
include($MYROOT."/mynewpage.php");

Then in mynewpage.php you can test for both variables:


if(isset($_GET['p_pc_title'])) {
$var = $_GET['p_pc_title'];
}
else if(isset($p_pc_title)) { //this should become true with above example
$var = $p_pc_title;
}

I hope this makes some sense. :)

Good luck!

dunhill

10:19 pm on Dec 16, 2006 (gmt 0)

10+ Year Member



Sorry might be a bit unclear, this works for me 100%

<?
@include("http://www.mywebsite.com/advertising/mynewpage.php?p_pc_title=$var_title");
?>

How do I change this to be relative? I have tried everything.

Please Help.

Achernar

11:18 pm on Dec 16, 2006 (gmt 0)

10+ Year Member Top Contributors Of The Month



There is a big difference between
include("/advertising/mynewpage.php?p_pc_title=$var_title");
and
include("http://www.mywebsite.com/advertising/mynewpage.php?p_pc_title=$var_title");

In the first syntax php access the local filesystem. This is why you can't pass arguments. Since it's not a http query but a file access, the filename is "mynewpage.php", not "mynewpage.php?p_pc_title=$var_title"

The second syntax forces php to use what it calls a "URL wrapper" (which is usually not enabled on most rented servers). PHP uses a http connection to get a copy of the code. In this case you have several potential problems.

  • If the connection if not possible at a specific time, your script (the one that is including) will fail.
  • This include process depends on the connection capabilities of the server (could be very slow).
  • Your main script can't share variables with the included script (they are not run on the same host), unless (I've not verified) the server hosting the included script outputs valid php code -- a correctly configured server will pass .php files to its php interpreter which in turn will output html (unless the script itself writes php code).
  • If the included script is hosted on the same server, it will put more pressure on the apache process, and could also consume more bandwidth (if you're on a rented server with monthly bandwidth).
  • One day, for security reasons, the server configuration might forbid this kind of URL wrapper.

    What you really want is syntax #1. If you need to pass arguments maybe you should use an initializing function after including the script.

    include("/advertising/mynewpage.php");
    newpageInit($var_title);

    or set a variable before including;

    $p_pc_title=$var_title;
    include("/advertising/mynewpage.php");
  • dunhill

    2:04 am on Dec 17, 2006 (gmt 0)

    10+ Year Member



    Thank you very much, I have learnt something new today.

    Best Regards!