Been trying to get this conditional working for HOURS!

Discussion in 'PHP' started by dp-user-1, Mar 29, 2007.

  1. #1
    SOLVED

    The problem was another aspect of the script refreshing the page to load a cookie. The Redirect command was lost in the process.

    Thanks to all who helped!

    -------------------------

    if ( isset($_GET['Redirect']) && $_GET['Redirect'] = "1" )
    {
    	?>
    	<script language="JavaScript" type="text/javascript">
    	<!--
    	location.replace('<?php echo $PHP_SELF.'?ShowProducts=1&'.CART; ?>');
    	//-->
    	</script>
    	<?php
    }
    
    PHP:
    Basically, when a user visits "page.php?Redirect=1," I want the script to perform a redirect. In this configuration it NEVER works. In others, it redirects regardless of what "Redirect" is set to or if it even exists. Still other scenarios cast it into an infinite loop.

    I've tried switch statements, I've tried every possible combination of =, ==, quotes, no quotes, etc., and I've just about had it.

    Any suggestions?

    Thanks,
    Peter
     
    dp-user-1, Mar 29, 2007 IP
  2. JoshuaGross

    JoshuaGross Peon

    Messages:
    411
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Definitely change it to....

    if ( isset($_GET['Redirect']) && ((int)$_GET['Redirect']) == 1 )
     
    JoshuaGross, Mar 29, 2007 IP
  3. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #3
    :( Still doesn't work.
     
    dp-user-1, Mar 29, 2007 IP
  4. loibe

    loibe Peon

    Messages:
    6
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hi,

    do you really need to have those js?

    maybe..

    <?
    if(isset($_GET['Redirect'])):
    if($_GET['Redirect'] == 1)
    header('location: '.$_SERVER['PHP_SELF'].'?ShowProducts=1&'.CART);
    endif;
    ?>
     
    loibe, Mar 29, 2007 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Three things that I see wrong with the line:
    
    if ( isset($_GET['Redirect']) && $_GET['Redirect'] = "1" )
    
    PHP:
    1) You're assigning to your get parameter, not comparing. Use double equals.
    2) You're comparing to a string, when I believe you really want to compare to a number. I may be wrong about that, but either way, get used to NOT putting quotes around numbers when you want to treat them as numbers. PHP normally deals with it, but will sometimes choke.
    3) Order of operations: not sure, but maybe your && has a higher precedence than the comparison. Use brackets and force the order.

    So...

    I'd try something like:
    
    if ( isset($_GET['Redirect']) && ($_GET['Redirect'] == 1) )
    
    PHP:
    instead.
     
    TwistMyArm, Mar 29, 2007 IP
  6. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    :( Still no luck, guys...

    Does $_GET have to involve a form? I'm trying to achieve the aforementioned effect when Redirect=1 is added to the URL.
     
    dp-user-1, Mar 29, 2007 IP
  7. JoshuaGross

    JoshuaGross Peon

    Messages:
    411
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Try print_r($_GET);

    $_GET is all the variables passed from the URL - or at least, it should be. Sometimes, but rarely, configurations are different.
     
    JoshuaGross, Mar 29, 2007 IP
  8. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It doesn't require a form, no. So long as you use the same case, you should be fine.

    It wouldn't hurt to try dumping your GET variable and see what it says is there... try adding:
    print '<pre>';
    print_r($_GET);
    print '</pre>';

    to see what PHP thinks is in the GET.
     
    TwistMyArm, Mar 29, 2007 IP
  9. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #9
    Good idea.

    It shows an array, but for some reason Redirect isn't in there, even when I manually type it...
     
    dp-user-1, Mar 29, 2007 IP
  10. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #10
    Solved, thanks guys. See first post for details.
     
    dp-user-1, Mar 29, 2007 IP