Does PHP know what page it's on?

Discussion in 'PHP' started by Will.Spencer, Jun 6, 2006.

  1. #1
    I am using this snippet of JavaScript on one of my web sites:

    <script language='javascript'>document.write("<a href='http://www.digg.com/submit?phase=2&url="+encodeURIComponent(document.location.href)+"' rel='nofollow'>Digg it</a>")</script>
    Code (markup):
    This JavaScript is useful, because it makes it possible for site visitors to Digg each of the pages on this site and I don't have to hard-code the URL of each page into the page itself.

    Is this possible with PHP?

    I am using .shtml (shared html) pages. Is it possible to embed a PHP script that will offer the same functionality as this JavaScript?

    I would prefer to do this function server-side instead of client-side.
     
    Will.Spencer, Jun 6, 2006 IP
  2. sgthayes

    sgthayes Peon

    Messages:
    171
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    look at this page from the php docs.
    You should be able to use $_SERVER['REQUEST_URI'] or $_SERVER['SCRIPT_NAME'] or __FILE__

    Have a look and test it out.
     
    sgthayes, Jun 6, 2006 IP
    Will.Spencer likes this.
  3. Will.Spencer

    Will.Spencer NetBuilder

    Messages:
    14,789
    Likes Received:
    1,040
    Best Answers:
    0
    Trophy Points:
    375
    #3
    So, in my primitive PHP, it would look like this:
    <?php
    
    echo "<a href=http://www.digg.com/submit?phase=2&url=http://";
    echo $_SERVER['SERVER_NAME']; 
    echo $_SERVER['REQUEST_URI']; 
    echo ">Digg it</a>";   
    echo "<br>"; 
    
    ?>
    
    Code (markup):
    Thanks Sergeant Hayes!
     
    Will.Spencer, Jun 6, 2006 IP
  4. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You would be better using your database to populate the title and story content as well rather than just the basic url.

    Check some of the news items on my phone site (see recent blog link above) to see what I mean.
     
    mad4, Jun 6, 2006 IP
    Will.Spencer likes this.
  5. Will.Spencer

    Will.Spencer NetBuilder

    Messages:
    14,789
    Likes Received:
    1,040
    Best Answers:
    0
    Trophy Points:
    375
    #5
    What database? :eek:

    What are you using to build the code behind that pretty little tower of graphics in the lower right hand corner of your blog? Can you send me the code snippet?

    What is the PHP variable for a page title?
     
    Will.Spencer, Jun 6, 2006 IP
  6. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #6
    All the data on my pages is build from a database, is your site database driven?

    If its not then my method won't help I'm afraid.

    The graphics on the right hand side are not auto generated, they are just graphics to allow people to sign up for my feeds.
     
    mad4, Jun 6, 2006 IP
  7. Will.Spencer

    Will.Spencer NetBuilder

    Messages:
    14,789
    Likes Received:
    1,040
    Best Answers:
    0
    Trophy Points:
    375
    #7
    Most of my sites are flat-files.

    Can you send me the code that is called by each button?

    It might make writing some of this PHP easier. :)

    Thanks!
     
    Will.Spencer, Jun 6, 2006 IP
  8. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #8
    There is no code behind the buttons, its all in the html of the page. They are simply links to external sites.

    Feel free to copy the html if you wish, I copied it from somebody else anyway.
     
    mad4, Jun 6, 2006 IP
  9. Will.Spencer

    Will.Spencer NetBuilder

    Messages:
    14,789
    Likes Received:
    1,040
    Best Answers:
    0
    Trophy Points:
    375
    #9
    Ahhh... I'm avoiding hard-coding the page names in HTML. :)
     
    Will.Spencer, Jun 6, 2006 IP
  10. kLdd15

    kLdd15 Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    need to rawurlencode() those variables if they have non-letters in them.
     
    kLdd15, Jun 6, 2006 IP