Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: redirect using header()

  1. #1
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default redirect using header()

    Hi,

    I want to go back to a previous webpage after I check the status of a user. I can use
    Code:
    header("location: index.php")
    to go to a main page, but when I use something else by either assigning a path and page to a variable inside a page, as
    Code:
    header("location: ".$where)
    or using the system as
    Code:
    header("location: ".$SERVER['SCRIPT_NAME'])
    it doesn't work. How can I do this?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You shouldn't use relative URLs in a Location header.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    Well, I've read that it should be something like
    Code:
    $host  = $_SERVER['HTTP_HOST'];
    $uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extra = 'mypage.php';
    header("Location: http://$host$uri/$extra");
    The problem is not with that, but getting it to actually work with a variable. I can't seem to use either of the two examples in first post or
    Code:
      header("location: $where");
    but the first example works fine when its a literal. I'll work on the rest to clean it up later after I get this tiny bit correct.

  4. #4
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    BTW, am using php 4.4.2 and mysql 5.0 (not my choice).

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, that's fine.

    Make sure the variable contains what you think it does -- echo it somewhere.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    yes, I'll do that - good idea, because when I use this:
    Code:
    	$host  = $_SERVER['HTTP_HOST'];
    	$uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    	header("Location: http://$host$uri/$where");
    it redirects me out of the server and to http://www.noaa.gov/

  7. #7
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    Ok, I've been wight with getting the page to redirect even though the code was changed back to its simplest form. Redirection was being done even though the code said not to, even after restarting the browser and clearing the cache, cookies.

    So, the question becomes, how do I redirect back to the first (calling) page, what ever it may be? This is getting so frustrating that I'm going out for chinese food! (and in Harlem NYC, that's bad!) this has been bugging me for two days! The code executes so very fast that nothing is/can be echoed.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Comment out the redirection line so you can see what the echo says?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    my variable is still null. Even when I try to use a session variable as in:
    Code:
    session_start(); 
    session_register("where"); 
    $where = "http://earth.engr.ccny.cuny.edu/noaa/wc/Lidar/chainedmenu2.php";
    What can I do to get back to the original page?

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If your variable is null, that would explain why the problem.

    Where do you define it? How do you retrieve it?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •