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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP problem involving redirection and an array

    I'm pretty sure that this is a simple problem to fix, but I don't have the necessary expertise. And after about 4 hours of trawling the net, I can't find a solution.

    I am conducting an experiment and need to randomly redirect participants to one of three conditions (I.e. web pages). Someone from these forums was lovely enough to help me with this a while ago, and I managed to get the code to work beautifully. that thread is here: http://www.codingforums.com/showthre...05#post1203705

    The code uses a .txt file containing numbers "1", "2" and "3" in random order. I am doing it this way for good reason - I need my participants to be redirected according to a number list that I made myself.

    The problem is, the code works fine on my laptop, but now that I've uploaded the website to a host, it keeps coming up with a 404 error message. I think I know what the problem is: the script (below) redirects people to pages that are named "1" "2" or "3". However, it does NOT redirect to pages named "1.php", "2.php" and "3.php".

    How do I make the script below do this? I.e, how do I assign the values "1", "2" and "3" in the .txt file to "1.php", "2.php" and "3.php"? Here's the code:

    <?php
    $page = file('test.txt', FILE_IGNORE_NEW_LINES);
    $useThis = $page[0];
    $love = array_shift($page);
    array_push($page, $useThis);
    $arr = $page;
    $fp=fopen("test.txt","w");
    foreach($arr as $key => $value){
    fwrite($fp,$value."\n");
    }
    header("Location: $useThis");
    ?>


    I really hope someone can help me with this - I need to get my study online asap!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,301
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Try this
    replace
    Code:
    header("Location: $useThis");
    with
    Code:
    header("Location: $useThis".'.php');

  • Users who have thanked sunfighter for this post:

    sir_jeans (04-19-2012)

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you - that solved the problem and it all works fine now. Your a legend!


  •  

    Posting Permissions

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