Asked  7 Months ago    Answers:  5   Viewed   32 times

I have to show a page from my php script based on certain conditions. I have an if condition and am doing an "include" if the condition is satisfied.

if(condition here){
  include "myFile.php?id='$someVar'";

Now the problem is the server has a file "myFile.php" but I want to make a call to this file with an argument (id) and the value of "id" will change with each call.

Can someone please tell me how to achieve this? Thanks.



Imagine the include as what it is: A copy & paste of the contents of the included PHP file which will then be interpreted. There is no scope change at all, so you can still access $someVar in the included file directly (even though you might consider a class based structure where you pass $someVar as a parameter or refer to a few global variables).

Wednesday, March 31, 2021
answered 7 Months ago

So if anyone ever stumbles on this forum because they are having the same issue let me explain what and why it went wrong.

If you include a function not in your directory(e.g c:// or file://) but instead include using http. The include can only return what was echoed in the file, but something like a variable or function will not be shown. So always include functions and variables through a directory

Wednesday, March 31, 2021
answered 7 Months ago

If you need to determine the base path of a set of scripts, you should not rely on the "current working directory." This can change from executing environment to executing environment.

Instead, base it on a known path.

/includes/class_bootstrap.php knows that it's going to be one directory down from where the base path is going to be, so it can do this:

define('CWD', realpath(dirname(__FILE__) . '/../') );

dirname gets the directory name given in the passed string. If __FILE__ returns C:/wamp/www/vb4/plugins/includes/class_bootstrap.php, then dirname will return C:/wamp/www/vb4/plugins/includes. We then append /../ to it and then call realpath, which turns that relative .. into a real directory: C:/wamp/www/vb4/plugins


From that point forward, CWD will operate as you expect. You can require_once CWD . '/includes/init.php' and it will correctly resolve to C:/wamp/www/vb4/plugins/includes/init.php

Also, this may sound stupid but "vb4" may be referring to vBulletin 4, in which case your plugin may already have access to the configuration information that it exposes, including handy things like paths. This may make this entire exercise unnecessary. I intentionally know nothing about vB, otherwise I would point you at their dev docs.

Wednesday, March 31, 2021
answered 7 Months ago

to finally get to run this idea was necessary to double GET request by .ajax

function GetForms(id){
        type:   "GET",
        dataType: "json",
        url:        "index.php",
        data:   {idfield:id},
        error: function(xhr,status,error){alert(error);},
        success:    function(data) {
        type:   "GET",
        url:        "index.php",
        data:   {idform:id},
        error: function(xhr,status,error){alert(error);},
        success:    function(response) {

and the PHP file:

        $return = json_encode(array('title'=>'titulo nuevo'));
        echo $return;
        $data   =       $this   ->  AJAXFORM        ->  GetHTMLAjaxForm();
        echo $data;

actually what I want is to simplify the amount of request to the server, and to take control of this would be very important.

if someone has a better idea favort communicate in this post.

with this working part, now lies in a new problem listed below:

Wednesday, March 31, 2021
answered 7 Months ago

The __FILE__ constant returns the full path and filename of the file containing that line. It is possible for this file to be located inside a directory other than the current working directory. In your particular example you can use this:

include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'htmlPurifierAndMarkdown.php';

An example of what might be going on is as follows:

  1. The file being executed is /
  2. Current working directory is /
  3. The index file includes /
  4. The above file attempts to include ../htmlPurifierAndMarkdown.php
  5. The .. is translated relative to current working directory and not relative to the included file. Thus you end up with / and a nasty error message.
Saturday, May 29, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :