SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    England
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting script title

    I have a main toolbar of just pictures that link to each area of the site. I want this displayed on every page. Firstly is it sensible to have it as an include script to avoid putting the code in every script?

    Secondly, I want the image to change slightly depending on what page the person is on. Is there a function or something that will return the title of the script so I can say if $scriptname = "home.php" then display different image.

    Cheers,

    James.

  2. #2
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    firstly: yes, it makes a lot of sense to keep your recurring elements (like the navigation bar) in a separate file and only include() them in other pages...if you need to change a link in there, you only edit the single file, rather than having to edit 100s of pages

    secondly: you "could" use $PHP_SELF, or you could try $SCRIPT_FILENAME, $REQUEST_URI and (in the extreme case) $PATH_TRANSLATED.

    e.g.

    PHP Code:
    if $PHP_SELF=='home.php' ... 
    Last edited by redux; Apr 23, 2002 at 06:25.
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  3. #3
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just so you know, all the variables listed above won't work without global variables set to on.

    If they're not, you need to use $HTTP_SERVER_VARS["PHP_SELF"] or if you have php 4.1.0 or later then you can use $_SERVER["PHP_SELF"]

    Also, from what i can tell, every kind of self identifying server variable includes the path, so you'd need to strip that out.
    That could be done by using explode() on the path/filename with the / as the seperator, and using the resulting array in a loop, find the number of entries in the array, and grab the last one, and then you'll have your scriptname.

    Or you could just put the filename in a variable at the top of each script
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    England
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers, instead of using a loop could I not just do this:
    PHP Code:
     $scriptarray explode("/"$PHP_SELF);
    $arraysize sizeof($scriptarray);
    $scriptname $scriptarray[$arraysize-1]; 
    Is there any reason to choose this over a loop or vice-versa?
    Doing this or a loop means I have to add this code to each page though. For example if I have header.php included in home.php and search.php (there are lots more as well) I have to put the above code in home.php, search.php and all the others. Is there a way that I can have some code like that in header.php so it realises what script is calling it?
    Sorry if I didn't make myself completely clear in the first place.

    Cheers,

    James

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use basename():

    PHP Code:
    $file basename($_SERVER['PHP_SELF']); 

    P.S. thanks for mentioning about global variables Defender.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •