Results 1 to 8 of 8

Thread: header redirects

  1. #1
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default header redirects

    Folks, I have a (working) PHP file that begins as follows:
    Code:
    <?php
      header('Location:http://www.MarainLaw.com/page.php?he...-jersey&#39;);
      die();
    
    switch ( $here ) {
    
    case 'index':
      header('Location:http://www.MarainLaw.com/page.php?he...-jersey&#39;);
      die();
      break;
    
    case 'prostitution-lawyer-nj':
      header('Location:http://www.marainlaw.com/page.php?here=prostitution');
      die(); 
      break;
    
    default:
      header('Location:http://www.MarainLaw.com/page.php?he...-jersey&#39;);
      die();
    .
    .
    .
    }
    Now I realize that, as written, I never reach the switch statement, but that's not my question. My question is this: Can the header redirect be selectively chosen using the switch statement (as I tried to do after line three)? That is, how, if at all, can I (omit the first header and die statements to just) redirect to a selected target page using the switch statement? When I try to implement it using the switch statement, I get a page-unavailable error message.

    A.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Those entities &#39; shouldn't be there, those need to be literal single quotes. There could be other problems. But to answer the underlying question, yes - a different page header redirect should be able to be sent based upon a value. However there's probably a better way to do that, like setting the page to be rediected to based upon that value and having only one redirect header. Also be aware that you cannot write to the page before invoking the header. Well you might be able to, but usually not. Another thing, a break after die is superfluous.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This works "here" (pun intended):

    PHP Code:
    <?php
    $here 
    = isset($_GET['here'])? $_GET['here'] : '';

    switch ( 
    $here ) {

    case 
    'index':
      
    header('Location:http://www.MarainLaw.com/page.php?here=sexual-assault-in-new-jersey');
      die();

    case 
    'prostitution-lawyer-nj':
      
    header('Location:http://www.marainlaw.com/page.php?here=prostitution');
      die(); 

    default:
      
    header('Location:http://www.MarainLaw.com/page.php?here=sexual-assault-in-new-jersey');
      die();
    }
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    marain (09-27-2014)

  5. #4
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    John, two issues exist, one corresponding to each of your much appreciated replies. Anent your first reply, the text that I uploaded did not contain &#39; characters. They appear to have been converted from apostrophes at some point in the message uploading process. Anent your second reply, I do understand (and heartily approve of) the isset test you provided. I'm not sure I can use it in my code: The problem is that I am maintaining a somewhat (to me) sophisticated site that I did not create. I have looked all over, in vain, to find where $here obtains its value. I'll keep looking. In the meanwhile, however, I lack confidence that I can just assume I'll find the current value in $_GET['here'. One last observation: I can't use your suggested technique for having just a single redirect header because multiple values of $here (in some cases) will be sent to a single location.

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You don't need isset. You do have to guarantee that $here equals something before you start switching it. And you have to make sure that all of the possible pages you would be redirecting to exist. But also keep in mind that, if the server or network is experiencing a problem, you can get a page unavailable and/or other errors even when all the code is in order. You only need one redirect, but that's a fine point. You could switch to choose the page and then redirect to it:

    PHP Code:
    <?php
    $here 
    = isset($_GET['here'])? $_GET['here'] : '';

    switch ( 
    $here ) {

    case 
    'index':
      
    $page 'http://www.MarainLaw.com/page.php?here=sexual-assault-in-new-jersey';
      break;

    case 
    'prostitution-lawyer-nj':
      
    $page 'http://www.marainlaw.com/page.php?here=prostitution';
      break; 

    default:
      
    $page 'http://www.MarainLaw.com/page.php?here=sexual-assault-in-new-jersey';
      break;
    }
      
    header('Location:' $page); // only one redirect
      
    die();
    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #6
    Join Date
    Apr 2012
    Location
    Central New Jersey
    Posts
    286
    Thanks
    95
    Thanked 3 Times in 3 Posts

    Default

    John, that is absolutely phenominal! Thanks again.

    A.

  8. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I take one part of that back. At least for my localhost PHP server, $here does not need to equal anything before it's switched. If it doesn't, then the default is used.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #8
    Join Date
    Oct 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Thank you for you help

    occur the same problem,Thank you for your help

Similar Threads

  1. Accordion header anchor link work while header is expanded
    By ngguh in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 05-10-2012, 07:11 PM
  2. On scroll header collapse to smaller sticky header
    By danielmeade in forum JavaScript
    Replies: 3
    Last Post: 07-17-2011, 12:28 PM
  3. redirects
    By lord_havoc in forum JavaScript
    Replies: 5
    Last Post: 08-13-2007, 08:02 PM
  4. Redirects and SEO
    By swiftmed in forum HTML
    Replies: 3
    Last Post: 05-24-2006, 12:33 PM
  5. Redirects
    By Freeman in forum HTML
    Replies: 1
    Last Post: 11-28-2005, 05:32 PM

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
  •