Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Loading a webpage based on PHP Variable Value

  • 20-08-2014 03:00PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 2,919 ✭✭✭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, Registered Users 2 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