Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Unhappy Using PHP header as a variable

    Is it possible?

    I want to check to see a users browser. If it is FF, direct them somewhere.

    <?php
    $redirect ='header( \'Location: http://localhost/portfolio/blog\' ) ';
    ?>


    <?php
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')
    echo $redirect
    ?>
    I know you can't output any text before a header, and I havn't. What comes up in my browser is

    header( 'Location: http://localhost/portfolio/blog' )
    Last edited by Psirus; 04-05-2008 at 06:27 PM. Reason: Typo

  • #2
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    Echo is used to output text. You want to call the header as a function. Like so:

    Code:
    <?php
    $redirect = 'Location: http://localhost/portfolio/blog';
    ?>
    
    
    <?php
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')
       header($redirect);
    ?>
    However this is untidy code you could shorten the whole thing to be inside only one <?php?> tag and without using a variable:

    Code:
    <?php
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')
       header('Location: http://localhost/portfolio/blog');
    ?>
    Last edited by flynch01; 04-05-2008 at 06:55 PM.

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I get you on the echo, thanks

    I get this error when using the bottom code you posted:

    Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\portfolio\index.php on line 3

  • #4
    Regular Coder
    Join Date
    Mar 2007
    Posts
    357
    Thanks
    46
    Thanked 22 Times in 21 Posts
    Sorry went for dinner. Yeah I only had a quick glance before so I didn't spot it. You opened two parenthesis and only closed one:

    This:

    Code:
    <?php
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')
       header('Location: http://localhost/portfolio/blog');
    ?>
    Becomes:

    Code:
    <?php
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
       header('Location: http://localhost/portfolio/blog');
    ?>

  • Users who have thanked flynch01 for this post:

    Psirus (04-05-2008)

  • #5
    New Coder
    Join Date
    Aug 2005
    Location
    Groningen, Netherlands
    Posts
    57
    Thanks
    0
    Thanked 6 Times in 6 Posts
    .. and don't forget: you cannot output anything before the header. So be careful that you don't type any spaces or anything before the starting "<?php".

    Second, to be sure, always use an
    Code:
    exit(0);
    after using the header. This prevents the script from being parsed if the header does not do its job.

    good luck!

  • Users who have thanked jaap for this post:

    Psirus (04-05-2008)

  • #6
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thanks guys mucho love!

    Basically I just made a portfolio page, which does not work what-so-ever in IE. So if they have firefox they get redirected, if not then they get told off.

  • #7
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    So, instead of fixing your page to work in IE, you took additional steps to alienate the largest percentage of web users and, in the same stroke, effectively killed any favorable SE treatment of your page.

    Good luck with it, but why would you bother?

  • Users who have thanked PappaJohn for this post:

    oesxyl (04-05-2008)

  • #8
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by PappaJohn View Post
    So, instead of fixing your page to work in IE, you took additional steps to alienate the largest percentage of web users and, in the same stroke, effectively killed any favorable SE treatment of your page.

    Good luck with it, but why would you bother?

    I tried.
    It has too many issues.

    Basically I created a site blah blah, then I put a section /blog/ in there and installed wordpress, edited the css etc. so it works fine in FF, and every other browser.

    I figured out some things, but there are too many to deal with.

    I know it is bad... I just don't know what else to do.

    What is SE treatment?

  • #9
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    True, IE causes seemingly no end of problems, but ignoring IE doesn't make the problems go away. IE is firmly entrenched and is not going to disappear (at least not anytime soon).

    I guess I just don't get the reasoning behind going through the trouble to put up a website, especially one with a blog which you'll, presumably, be updating only to have it inaccessible to the majority of users.

    I know it is bad... I just don't know what else to do.
    If you're serious about fixing it, post in the HTML forum. There are plenty of helpful, knowledgeable folks that hang around.

    What is SE treatment?
    Search Engines. Since the search engines don't browse with Firefox, all they'll ever index is your rant about not using IE - not very conducive to attracting visitors.

  • #10
    New Coder
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    10
    Thanked 0 Times in 0 Posts
    By all means I don't intend this to be a final solution to my problem.

    Thank you very much for your advice. I guess I will hop on over to the HTML and CSS guys and ask them for a hand.

  • #11
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    You're welcome. I think that's a better approach.


  •  

    Posting Permissions

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