I tried that. It didn't work.
Main Topics
Browse All TopicsI'm trying to use PHP_SELF in a class but it isn't working. I have two files:
classtest.php
page.inc
In classtest.php I create a new web page using the Page class which is defined in page.inc. In page.inc I have a function SetFilename() that gets the basename of the file (I found this function here on ee). Previously, I was using this function directly in a php script file that created a web page (without the use of a class) and it worked. It isn't working using a class.
The value I'm trying to get for the $this->filename variable is: classtest
I anticipated a problem with this but thought that I just might get the wrong value for $this->filename(page from page.inc). But I'm not getting anything.
classtest.php
--------------
<?php
require('page.inc');
$classtestpage = new Page();
$classtestpage -> Display();
?>
page.inc
---------
<?php
class Page
{
//class Page's attributes
var $content;
var $title = 'Test Title';
var $keywords = 'Test Keyword, Test Keyword 2';
var $filename;
//class Page's operations
function SetFilename()
{
$brk_filename = explode(".", basename($PHP_SELF));
$num_part = count($brk_filename);
if( $num_part >1)
unset($brk_filename[$num_p
$this->filename = implode(".", $brk_filename);
}
function Display()
{
echo "<html>\n<head>\n";
$this->DisplayTitle();
$this->DisplayKeywords();
echo "</head>\n<body>\n";
$this->DisplayFilename();
echo "this is a test from the display function";
echo "</body>\n</html>\n";
}
function DisplayTitle()
{
echo '<title>'.$this->title.'</
}
function DisplayKeywords()
{
echo "<META name =\"keywords\" content=\"".$this->keyword
}
function DisplayFilename()
{
echo $this->filename;
}
}
?>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>Why not use this:
>
>function SetFilename()
> {
> $this->filename = basename($PHP_SELF,".php")
> }
The function I'm using will work for any filename, with or without an extension, and it works when I'm not using a class. The function you provided also does not work in the class.
>You have to use the __FILE__ constant
This also didn't work in the class.
I've figured out the problem -- I wasn't calling the SetFilename() function anywhere. The following is the solution that works. Also, see at the bottom an alternative SetFilname() function.
classtest.php
--------------
<?php
require('page.inc');
$classtestpage = new Page();
$classtestpage -> Display();
?>
page.inc
---------
<?php
class Page
{
//class Page's attributes
var $content;
var $title = 'Test Title';
var $keywords = 'Test Keyword, Test Keyword 2';
var $filename;
//class Page's operations
function Page()
{
$this->SetFilename();
$this->SetTitle();
$this->setKeywords();
}
function SetFilename()
{
global $PHP_SELF;
$brk_filename = explode(".", basename($PHP_SELF));
$num_part = count($brk_filename);
if( $num_part >1)
unset($brk_filename[$num_p
$this->filename = implode(".", $brk_filename);
}
function SetTitle()
{
$this->title = 'Title from SetTitle function';
}
function SetKeywords()
{
$this->keywords = 'Keywords from SetKeywords function';
}
function Display()
{
echo "<html>\n<head>\n";
$this->DisplayTitle();
$this->DisplayKeywords();
echo "</head>\n<body>\n";
$this->DisplayFilename();
echo "<br>";
echo "this is a test from the display function";
echo "</body>\n</html>\n";
}
function DisplayTitle()
{
echo '<title>'.$this->title.'</
}
function DisplayKeywords()
{
echo "<META name =\"keywords\" content=\"".$this->keyword
}
function DisplayFilename()
{
echo $this->filename;
}
}
?>
Alternative SetFilename() function
--------------------------
function SetFilename()
{
$brk_filename = explode(".", basename($_SERVER['PHP_SEL
$num_part = count($brk_filename);
if( $num_part >1)
unset($brk_filename[$num_p
$this->filename = implode(".", $brk_filename);
}
If you use PHP_SELF you have to define it as global. Instead, you can use $_SERVER['PHP_SELF']. I had already tried using $_SERVER['PHP_SELF'] before posting this question, but it didn't work because I wasn't calling the SetFilename function. I'm new to PHP but from what I've read it seems that it would be better to use $_SERVER['PHP_SELF'].
That was the good one :)
Besides try to avoide register globals set to on.
Here's why:
http://us4.php.net/registe
>Besides try to avoide register globals set to on.
Do you mean:
Avoid going into php.ini and changing register_globals to 'on'? (I understand why this isn't good)
Or do you mean:
Avoid doing this in php scripts?
In other words, should I avoid declaring variables as 'global' in my scripts? I'm very new to PHP and don't have a handle on all the nuances yet.
Also, thanks for the link (http://us4.php.net/regist
I would recommend you to go to php.ini and set register_globals to OFF, this will avoid future problems when running your scripts on servers with newer versions of PHP (when register_globals is set to OFF).
Since $PHP_SELF is working for you, you have it set to ON (no good...)
It is good practice to use $_POST[], $_GET[], $_SERVER[] etc. collections instead of global variables.
Hope this helps
Instead of explodiing and imploding, it is easier to do:
$filename = substr( $file, 0, strrpos( $file, "." ) - 1 );
Register globals needs to be OFF! I would guess 90% of webhosts now have it set to off, and it is slated to be completely removed in future versions. Register globals is evil and just adds overhead anyway.
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Split: peyox {http:#13697957} & lokip {http:#13698554}
Any objections should be posted here in the next 4 days. After that time, the question will be closed.
Huji
EE Cleanup Volunteer
Business Accounts
Answer for Membership
by: peyoxPosted on 2005-04-03 at 22:30:14ID: 13695105
Since "register globals" setting is set to OFF by default in newer verisions of PHP, you should use:
$_SERVER['PHP_SELF']