Retrieving basename of site url using session variable

Discussion in 'PHP' started by rrn, Apr 15, 2009.

  1. #1
    hi

    am using the below shown code for getting index.php of www.test.com/index.php
    session_start();
    $pagename=pathinfo($_SERVER["PHP_SELF"]);
    $pagename=$pagename["basename"];
    $_SESSION['page']=$pagename;
    PHP:
    $_SESSION['page'] has the value index.php
    ie if i am on www.test.com/abc.php $_SESSION['page'] has the value abc.php

    it is working fine..
    but my problem is that.. if the url is www.test.com/index.php?catid=60
    the rest part after index.php ie '?catid=60' is not retrieved..

    how can i do that???
    please help..
    i will be very grateful..
    thank you
     
    rrn, Apr 15, 2009 IP
  2. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If I'm not mistaking it is basename which only takes the actual page and not the parameters

    You could do this:
    $url = $_SERVER["PHP_SELF"];
    $page = split("/", $url); //returns an array, we need last item of it
    $length = count($page);
    $_SESSION['page'] = $page[$length-1];
    Code (markup):
     
    fex, Apr 15, 2009 IP
  3. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Think this has to do with a bug of PHP_SELF not working right on older versions of Apache. Maybe try using $_SERVER['REQUEST_URI'] instead.
     
    JDevereux, Apr 15, 2009 IP
  4. jimbursch

    jimbursch Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you know catid is always going to be the only parameter, you might do this:

    session_start();
    $pagename=pathinfo($_SERVER["PHP_SELF"]);
    $pagename=$pagename["basename"];
    if (isset($_GET['catid'])) {$pagename .= '?catid='.$_GET['catid'];}
    $_SESSION['page']=$pagename;
     
    jimbursch, Apr 15, 2009 IP
  5. rrn

    rrn Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    JDevereux , thanks a lot . tried wat u said.. it worked..
    thanks once again..

    Thanks to all the others who gave suggestions .
    Really appreciate your willingness to help..

    thank u.......
     
    rrn, Apr 15, 2009 IP