Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Loading a webpage based on PHP Variable Value

Options
  • 20-08-2014 3:00pm
    #1
    Registered Users Posts: 2,484 ✭✭✭


    Hey guys,

    I'm just wondering is it possible to use PHP or something similar to force load a different HTML Page.

    For example, having a Random Number Generator on the page that would load at the first, and its would pull a number from 1 - 10 at random, if the value matches a set value, say 5, page_1.html would be loaded, whereas if anything else came up page_2.html would be loaded?

    This is what I have so far (not sure if this is even right, its only what I could piece together from resources online, and what I remember from the bit of php I did years ago.

    [PHP]<?php

    $var = 5;

    if (mt_rand(1,10) = $var) {
    echo "This should auto redirect to page_1.html";
    } else {
    echo "This should also redirect to page_2.html";
    }
    ?>[/PHP]

    Will this script work to generate and check the value? And, if so, how can I get the PHP to auto-redirect to the right HTML page. (Or, even if needed to display certain HTML Code within the page and I'll build the 2 pages within the PHP).

    Thanks a million guys, any help is really appreciated!

    -Snipe


Comments

  • Subscribers Posts: 1,911 ✭✭✭Draco


    You could use the header function and pass through the location of the page you want to go to.
    i.e.
    header('Location: http://localhost/page_2.html');
    exit;
    

    Just don't output anything before calling that. Also the reason to exit afterwards is that if you don't the rest of your script will be executed.


  • Registered Users Posts: 2,484 ✭✭✭The Snipe


    Seem to be getting a Syntax error from a colon somewhere on Line 8:

    [PHP]<?php

    $var = 5;

    if (mt_rand(1,10) = $var {

    // Redirect to page 1
    header("Location: http://localhost/page_1.html");
    die();
    } else {
    // Redirect to page 2
    header("Location: http://localhost/page_2.html");
    die();
    }

    ?>[/PHP]

    Line 8:
    [PHP] header("Location: http://localhost/page_1.html");[/PHP]

    I'll take another look though when I'm at home, just finished now!


  • Registered Users Posts: 2,786 ✭✭✭mightyreds


    Are you missing a parenthesis on the if statement and should it be ==$var


  • Subscribers Posts: 1,911 ✭✭✭Draco


    This line:
    if (mt_rand(1,10) = $var {
    

    should probably be:
    if (mt_rand(1,10) =[b]=[/b] $var[b])[/b] {
    

    (changes in bold)


  • Registered Users Posts: 2,484 ✭✭✭The Snipe


    Aha, thanks guys! Its been a good few years since I've done anything with PHP (At that, I think it was just the basic contact forms).

    Ye've been a great help! :)


  • Advertisement
Advertisement