Included files work around -- help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
galvar2bos
Forum Newbie
Posts: 5
Joined: Fri Aug 03, 2007 8:47 pm

Included files work around -- help

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Yes. As long as the function or class has been included somewhere before it is used it will work.
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Included files work around -- help

Post 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();
?>
galvar2bos
Forum Newbie
Posts: 5
Joined: Fri Aug 03, 2007 8:47 pm

Post 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 ??????????
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

@original poster: you never mentioned you were talking about remote includes.

Are the sites on the same server?
galvar2bos
Forum Newbie
Posts: 5
Joined: Fri Aug 03, 2007 8:47 pm

Post 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 ?!!!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Post Reply