?
Solved

Cannot get value from define function ?

Posted on 2008-11-08
11
Medium Priority
?
363 Views
Last Modified: 2012-05-05
hello everyone,
I try to get value from define function (core php)

I explain my problem :
I have 3 pages.
- index.php
- config.php
- footer.php

index.php :
i include the config.php file ( see code snippet )

config.php :
declare the constant SERVER_WEB by using define function. ( see code snippet )

footer.php : ( see code snippet )
try to show value of constant SERVER_WEB. and this is what i get :
SERVER_WEB

no error is reporting from php.
thanks for all,

//*************** index.php : 
<?
error_reporting(E_ALL);
 
session_start();
include('includes/config.php');
//(...)
?>
 
//*************** config.php : 
define('SERVER_WEB',$_SERVER['SERVER_NAME']);
 
//*************** footer.php : 
<?=SERVER_WEB?>

Open in new window

0
Comment
Question by:beatboxradio
  • 6
  • 3
  • 2
11 Comments
 
LVL 11

Assisted Solution

by:jausions
jausions earned 360 total points
ID: 22913110
error_reporting(E_ALL) may not be enough if display_errors if turned off, unless you're looking at the web server's log file.

Add ini_set('display_errors', true) to make sure errors are showing.

You could have a wrong include for instance, or some other errors that are not displayed.
0
 
LVL 5

Author Comment

by:beatboxradio
ID: 22913214
ok i've added new things...

into index.php :
error_reporting(E_ALL);
ini_set('display_errors', true);

look into logs of apache and php, and no error is showing.

i will add a new information :
when i call the page footer.php, i use this :
include(HTML_PATH.'footer.php');

the page is called successfully but the variable SERVER_WEB has no value.

also into php.ini, this is what i get :
allow_url_fopen = On
allow_url_include = On

display_errors = On
display_startup_errors = On
log_errors = On
error_reporting = E_ALL & E_STRICT


0
 
LVL 11

Assisted Solution

by:jausions
jausions earned 360 total points
ID: 22913249
Double check the config.php you're loading is really the one you expect. For instance is it really in the includes/ sub-folder etc...

An easy way to do check this is to intentionally create a parsing error in the config.php file.

Also make sure the config.php file doesn't overwrite error_reporting set in the index.php.

On a side note, I advise you to always use <?php instead of <? and use <?php echo instead of <?=

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Author Comment

by:beatboxradio
ID: 22913300
ok, i change again my things.

really strange...
//************ index.php
<?php 
error_reporting(E_ALL);
define('TEST','TTESTSETSETSETSET');
echo TEST;
 
//******** HTML_PATH works here, value is html/
include(HTML_PATH.'footer.php');
 
?>
 
//**************** footer.php
<?php echo TEST; ?>
 
 
//********** value of TEST show in index.php
/** TTESTSETSETSETSET
//********** value of TEST show in footer.php
/** TEST

Open in new window

0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 640 total points
ID: 22914041
Maybe short_open_tag is disabled on your server?

http://php.net/manual/en/ini.core.php#ini.short-open-tag

In your last example, how is the footer.php called? Directly in the browser, or included from index.php?
The value of an undefined constant will be the name of the costant, i.e. if you did not define TEST, the output of echo TEST; should be "TEST". It should also give a warning about undefined constant.

http://php.net/constants

You can use the defined() function to check if a constant is definied:

http://php.net/manual/en/function.defined.php
0
 
LVL 5

Author Comment

by:beatboxradio
ID: 22914266
for CXR :
the short_open_tag was ON.

but  I found something :

i call footer.php like that :

<? include(HTML_PATH.'footer.php'); ?>
( where HTML_PATH = http://localhost/beatboxradio_v6/html/ )

and like this way, the constant into footer.php TEST doesn't work.

if I replace HTML_PATH by subfolder " html/ " =
everything works fine.

i don't know why by using HTML_PATH the constant don't have the value because
the footer.php file load successfully by using HTML_PATH.

thx






0
 
LVL 5

Author Comment

by:beatboxradio
ID: 22914271
for CXR :
i call footer.php into index.php like :
<? include(HTML_PATH.'footer.php'); ?>

and TEST constant was defined into index.php  like :
define('TEST', 'TTESTSETSETSETSET' );

0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 640 total points
ID: 22914542
Try executing this:

echo phpversion().'<br />';
echo 'allow_url_fopen='.(ini_get('allow_url_fopen') ? 'on':'off').'<br />';
echo 'allow_url_include='.(ini_get('allow_url_include') ? 'on':'off').'<br />';

Including a file using http:// is only allowed if the version is later than 4.3 and both allow_url_* settings are on.

http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

A safer way to include is using $_SERVER['DOCUMENT_ROOT']:

include($_SERVER['DOCUMENT_ROOT'].'/beatboxradio_v6/html/footer.php'); 

Open in new window

0
 
LVL 5

Author Comment

by:beatboxradio
ID: 22914581
this is what i got :

5.2.5
allow_url_fopen=on
allow_url_include=on

thx for your new way to include !
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 640 total points
ID: 22914656
Using urls in the include path is for including code from remote servers. Using localhost won't work at all on my windows box, don't know why. I get a timeout. Apache error log says:

"failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."

...this is when php tries to connect to localhost over port 80...!?

As a consequence, I can not recreate your situation.
0
 
LVL 5

Accepted Solution

by:
beatboxradio earned 0 total points
ID: 22916682
ok, I found a solution :

"When you include it using a URL as above, the file is served from the
webserver which parses the php script as it has a php extension. With
the other extensions, the php code isn't parsed. Try looking at the
URL with a browser and you'll see what I mean.

Your include path should be a physical path to the file e.g:
include ('/usr/local/htdocs/includes/includes.php');"

by using, the physical path, everything works fine !

thx for all !
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question