Results 1 to 8 of 8

Thread: [RESOLVED] Back link in php

  1. #1
    Join Date
    Dec 2008

    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:
    if(isset($_GET['frompage'])) {
    $_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
    Behind you...
    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:
    if(isset($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], "your-site.com") !== FALSE) {
    '<a href="' $_SERVER['HTTP_REFERER'] . '">Back</a>';
        } else {
    '<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
    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.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

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

  5. #5
    Join Date
    Dec 2008
    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
    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.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  7. #7
    Join Date
    Dec 2008
    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
    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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.20874 seconds
  • Memory Usage 2,924KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates