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.

 Answers

62

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
 
pamelus
answered 7 Months ago
98

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
 
BetaRide
answered 7 Months ago
17

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

Phew.

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
 
Exoon
answered 7 Months ago
50

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

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

and the PHP file:

if($_GET){
    if(@$_GET['idfield']){
        $return = json_encode(array('title'=>'titulo nuevo'));
        echo $return;
    }
    if(@$_GET['idform']){
        $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:

https://stackoverflow.com/questions/32516272/html-inserted-with-ajax-not-work-with-other-js-script

Wednesday, March 31, 2021
 
kmunky
answered 7 Months ago
38

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 /example.com/public_html/index.php
  2. Current working directory is /example.com/public_html/
  3. The index file includes /example.com/public_html/includes/classes/genral.class.php
  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 /example.com/htmlPurifierAndMarkdown.php and a nasty error message.
Saturday, May 29, 2021
 
insomiac
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 :