Page 1 of 1

Included files work around -- help

Posted: Sat Aug 04, 2007 8:21 pm
by galvar2bos
Hi all ,

Again i have a question i will get to the point quickly i have 3 php files

1.php

Code: Select all

<?php
error_reporting(E_ERROR | E_PARSE);
define( 'IBIET', 1 );
include "3.php";
include "db.php";

?>
2.php

Code: Select all

<?php
error_reporting(E_ERROR | E_PARSE);

include "1.php";

$DB = new db;
echo "databe is ";
echo $DB->obj['sql_database'];
?>
3.php

Code: Select all

<?php


if ( ! defined( 'IBIET' ) )
{
	print "You cannot access this file directly.";
	exit();
}
$string = "waleed";

?>
and dp.php

Code: Select all

<?php

if ( ! defined( 'IBIET' ) )
{
	print "You cannot access this file directly.";
	exit();
}

class db {

    var $query_count    = 0;
    var $obj  = array (
        'sql_host'      => '127.0.0.1',
        'sql_user'      => 'root',
        'sql_pass'      => '12345678',
        'sql_database'  => 'latin1'
    );

    function connect()
    {
        @mysql_connect($this->obj['sql_host'], $this->obj['sql_user'], $this->obj['sql_pass']);
        @mysql_select_db($this->obj['sql_database']) or die(mysql_error());
    }
}


?>
what happens is 2.php is the main file when i try it it worx so the quiestion is can included files be used to call included functions in the included file ?!!!!!!!!!!!


regards

Posted: Sat Aug 04, 2007 10:18 pm
by Christopher
Yes. As long as the function or class has been included somewhere before it is used it will work.

Re: Included files work around -- help

Posted: Sat Aug 04, 2007 10:26 pm
by RobertGonzalez
galvar2bos wrote:the quiestion is can included files be used to call included functions in the included file ?!!!!!!!!!!!
This is an excellent example of a question that could have been answered by trying it.

page1.php

Code: Select all

<?php
function testIncludeFunction() {
  echo "I am in page1.php";
}
?>
page2.php

Code: Select all

<?php
include 'page1.php';
testIncludeFunction();
?>

Posted: Sun Aug 05, 2007 5:17 am
by galvar2bos
The real deal is here .... when i try this script it worx locally ... but say i have a nested include in my other site

example
first site

site-1-1.php

Code: Select all

<?php
                     $string = "hi there";
?>
site-1-2.php

Code: Select all

<?php
include "site-1-1.php";
echo "cooool -- :"
echo $string
?>
now lets include the file from first site to the file in 2nd site

2nd site

Code: Select all

<?php
include "http://1stsite/site-1-2.php"
echo "this is my site echo again ---:";
echo $string;
?>
this won't work why ????? ........... despite that including from other sites is bad

why isn't the code working and how to make it work ??????????

Posted: Sun Aug 05, 2007 5:50 am
by volka
Your php instance (A) sends a http request to the webserver at 1stsite. This webserver (even if it is physically the same) will start a new instance of php (B) and execute the script site-1-2.php. The output of (B) is sent as http response. (A) only includes the text (B) printed.

Posted: Sun Aug 05, 2007 12:56 pm
by RobertGonzalez
@original poster: you never mentioned you were talking about remote includes.

Are the sites on the same server?

Posted: Tue Aug 07, 2007 4:05 am
by galvar2bos
i though i have posted a replay ,anyway

No i am trying to walk around include function and see it from all views ....

the file isn't on the same server ....

so i can't include a php code because the server responce with the executed page not the code ,right ?

and there is no way around it ?

i have another include that confuses me .. it's remote too

why can't i include a defined('') protected page ... for example :

1 - call.php


Code: Select all

<?php
Define('Alo',True);
include "http://site.com/answer.php";

echo $ans;
?>
2 - answer.php

Code: Select all

<?php

if ( ! defined( 'Alo' ) )
{
	print "You cannot access this file directly.";
	exit();
}
$ans = "who is it?";
?>
is it the same problem the server first execute the php code and returns the "You cannot access this file directly." responce ?!!

how to make it work and ideas ?!!!

Posted: Tue Aug 07, 2007 6:02 am
by volka
galvar2bos wrote:is it the same problem the server first execute the php code and returns the "You cannot access this file directly." responce ?!!
More or less, yes.
the Define('Alo',True); defines the constant in php instance (A). The check for the defined constant takes place on the remote server, i.e. php instance (B) . The two instances do not share variables and/or constants.