SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirect to same page from where it is called

    Hi,

    Is there a way to redirect to same page from where action was trigerred?

    Example - I have page A. On button clicked I called let's say B.php on some different folder. Now I'm done with the stuff in B.php and want to redirect to A back.

    I mean is there a way where I can save the path of caller and reuse it later? I tried $_SERVER['REQUEST_URI'] but this provides the path to which it is called (destination).

    Also I'm trying to have common logic written in B.php which can be called from any other location. I can write this in A.php and create object for B.php and get the work done. But by doing this I will have to re-write code for some common functionalities.

    Thanks!

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    You could use $_SERVER['HTTP_REFERER'], but you can't really trust that as it's easily editable.

    You could also put the processing code from B.php into A.php, where the bulk of the code remains in B.php as a function. From A.php, call the function in B.php. Once that's all done, you're still in A.php and can display it's contents. No headers needed at all

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    926
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    If the user goes from A to B by submitting a form then you can do it easily by submitting the url of A in a hidden element:
    HTML Code:
    <input type="hidden" name="redirect" value="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']) ?>" />
    Then page B has the original url sent by page A. I often use this technique and it's very reliable.

    If you don't use forms then you can pass the original url from A to B in the link as a parameter:
    PHP Code:
    $link_to_B 'pageB.php?redirect=' urlencode($_SERVER['REQUEST_URI']); 
    You could also use cookies or sessions to store the url of A but then this won't work properly when someone has two tabs/windows open at the same time and go through these pages concurrently.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!


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
  •