• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

using PHP_SELF in a class

I'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_part-1]);
            $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.'</title>';
      }

      function DisplayKeywords()
      {
            echo "<META name =\"keywords\" content=\"".$this->keywords."\">";
      }

      function DisplayFilename()
      {
            echo $this->filename;
      }
}
?>
0
pacidev
Asked:
pacidev
2 Solutions
 
peyoxCommented:
Since "register globals" setting is set to OFF by default in newer verisions of PHP, you should use:

$_SERVER['PHP_SELF']
0
 
pacidevAuthor Commented:
I tried that. It didn't work.
0
 
JKlatteCommented:
Why not use this:

 function SetFilename()
     {
          $this->filename = basename($PHP_SELF,".php"); // returns "classtest";
     }
0
Independent Software Vendors: 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!

 
pacidevAuthor Commented:
>Why not use this:
>
>function SetFilename()
>     {
 >         $this->filename = basename($PHP_SELF,".php"); // returns "classtest";
 >    }

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.
0
 
RoonaanCommented:
You have to use the __FILE__ constant.

$this->filename = basename(__FILE__);

__FILE__ always holds the filename of the file the constant is used:

index.php:

echo __FILE__ ;//=> wwwhome/index.php
include 'test.php';

test.php:
echo __FILE__; => wwwhome/test.php

-r-
0
 
pacidevAuthor Commented:
>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_part-1]);
            $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.'</title>';
      }

      function DisplayKeywords()
      {
            echo "<META name =\"keywords\" content=\"".$this->keywords."\">";
      }

      function DisplayFilename()
      {
            echo $this->filename;
      }
}
?>

Alternative SetFilename() function
---------------------------------------
      function SetFilename()
      {
            $brk_filename = explode(".", basename($_SERVER['PHP_SELF']));
            $num_part = count($brk_filename);
            if( $num_part >1)
                  unset($brk_filename[$num_part-1]);
            $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'].

0
 
peyoxCommented:
That was the good one :)
Besides try to avoide register globals set to on.
Here's why:
http://us4.php.net/register_globals
0
 
pacidevAuthor Commented:
>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/register_globals). Very useful with regard to security.
0
 
peyoxCommented:
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




0
 
lokipCommented:
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.
0
 
hujiCommented:
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now