www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] Back link in php

  1. #1
    Join Date
    Dec 2008
    Posts
    132

    resolved [RESOLVED] Back link in php

    I am trying to get a back link to have two function. If it comes from a page in my website it will use the history.go(-1) function if it is from outsite it will go to a general page. I've tried the below script but it doesn't work.

    page 1 link <a href="page2.php?frompage=1">

    page 2 script and link.

    <a href="<?php echo $link ?>">Back to Page 1</a>

    PHP Code:
    <?php
    if(isset($_GET['frompage'])) {
        if(
    $_GET['frompage'] == 1) {
            
    $link "<a href='history.go(-1);'>";
        } else {
             
    $link "generalpage.php";
        }
    }
    ?>
    Last edited by phpnewbie08; 04-14-2014 at 01:40 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    862
    This appears to be the same question asked earlier, but this time in PHP rather than javascript. And so I'll treat it as such in my answer.

    You don't need to apply $_GET variables to links on your pages to do this. You can also check the referrer URL in PHP which will allow you to see which site the previous page was and thus you can determine what to do.

    PHP Code:
    <?php
        
    if(isset($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], "your-site.com") !== FALSE) {
            echo 
    '<a href="' $_SERVER['HTTP_REFERER'] . '">Back</a>';
        } else {
            echo 
    '<a href="http://www.your-site.com/general-page.php">Back</a>';
        }
    ?>
    I actually had the back button link to the actual previous url rather than having it set a link with 'history.go(-1)' because if you're going to use javascript links to navigate this way you can use the previous solution. This is designed to be a full PHP-based solution. And based on your code you can always set a '$link' variable instead of echoing out the <a> tag like I've done in my code here.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,312
    Note however that browsers are not required to send the referrer URL, so if it is critical to your application to know this, $_SERVER['HTTP_REFERER'] may not be the ideal solution. If it's not critical, then it's definitely a clean, simple way to deal with it. (I don't know what the odds are that it won't be included -- probably depends on browser, configuration/security settings, and/or proxy use/settings.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Dec 2008
    Posts
    132
    I figured it out thx.
    Last edited by phpnewbie08; 04-14-2014 at 06:52 PM.

  5. #5
    Join Date
    Dec 2008
    Posts
    132
    NogDog, if the referrer URL is not pass thru. Does it mean the link/button will just not work or will it give error message to the user?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,312
    If the user's browser is not sending the referrer header, then you'd fall through to the else block in Sup3rkirby's example, even if in reality you got there from a page on your site (since either the isset() would fail, or else the value of $_SERVER['HTTP_REFERER'] would be empty and wouldn't match). By testing for the the isset() first, though, he avoids any warnings/errors in the second part of the if() condition, so you won't have to worry about errors, and the link would always point to the default page in those cases.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Dec 2008
    Posts
    132
    Thx for the clarification NogDog. That will be fine for me. Thanks for the help Sup3rkirby and Nogdog. Appreciate it.

  8. #8
    Join Date
    Mar 2012
    Posts
    1,494
    As an alternative, you could set a GET or POST variable e.g. callfrom to identify the page (or section) of your site that the current page was called from. This has the advantages:

    1. A simple if(isset(callfrom)) should be true for an internal referral, but false for an external one.
    2. It does not rely on the system configuration.
    3. It is open to future enhancement, if at some point you want to handle some internal referrals differently from others.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles