jLynx
jLynx

Reputation: 1149

Is it possible to have a link invisible/hidden in webpage source but active when button click?

Hey guys I was wondering, is it possible to have a link invisible/hidden in webpage source but active when a button is clicked? Why do I want this? Because i'm creating a webpage sort of game but I don't want the user to be able to just right click and see the source code (I don't mean disabling right click) so they can just see the next URL and manually enter it into the web browser to get to the next page/level, I want it to be given to them when they finish the challenge and click the submit button. Sorry if I didn't explain this to easily, if you didn't quite understand, just say and i will try rephrase what i am trying to say :)

BTW preferably in Jquery including PHP

    <script language="JavaScript">
  function TheFunction(answer) {
    if (answer == "theanswer") {
      alert("Well done, time for level 2.");
      location.href = "<?php echo "hi"; ?>.php"; // <-- or somthing like this
    }
    else {
        alert("Wrong answer.");
    }
  }
</script>

Upvotes: 0

Views: 1159

Answers (5)

padarom
padarom

Reputation: 3648

You can use XMLHttpRequests (AJAX) to fetch the next URL from a PHP-script, like the following:

$urls = array(
    1 => 'myGame1.php',
    2 => 'thisIsPage2.php',
    3 => 'iLikeJavaScript.html'
);

if (isset($_POST['nextPageUrl']), is_int($_POST['nextPageUrl'])) {
    echo $urls[$_POST['nextPageUrl']];
}

In this example you have an array with all the URLs inside and echo just the one you need. Alternatively you are able to load the next page from the database, generate a new random file, whatever you need.

On your javascript side of things, you use something like this (assuming you are using jQuery, "normal" javascript is also possible, just a bit more complicated):

function TheFunction(answer) {
    if (answer == "theanswer") {
        alert("Well done, time for level 2.");
        $.ajax({
            url: 'yourPage.php', 
            data: { nextPageUrl: 2 }
        }).done(function (msg) {
            location.href = msg;
        });
    }
    else {
        alert("Wrong answer.");
    }
}

This is just an approach to the actual solution. Every client-side validation can be bypassed, therefore you might as well check the answer in the PHP-file, just to make sure someone isn't manually performing the AJAX request.

This could simply be done by modifying the PHP-file, and passing the answer to the script via ajax (expanding the data-object).

$urls = array(
    1 => array('url' => 'myGame1.php', 'answer' => 'theanswer'),
    2 => array('url' => 'thisIsPage2.php', 'answer' => 'theanswer2'),
    3 => array('url' => 'iLikeJavaScript.html', 'answer' => 'theanswer3'),
);

if (isset($_POST['nextPageUrl']), is_int($_POST['nextPageUrl'])) {
    if ($urls[$_POST['nextPageUrl']]['answer'] == $_POST['thisAnswer'])
        echo $urls[$_POST['nextPageUrl']]['url'];
}

Upvotes: 1

Lewis
Lewis

Reputation: 14876

Something like this may be what you're thinking about:

On server side, create a file JS.php containing your javascript code like this:

echo '
  function Try(answer) {
    if (answer == "theanswer") {
      alert("Well done, time for level 2.");
      location.href = "<?php echo "hi"; ?>.php"; // <-- or somthing like this
    }
    else {
        alert("Wrong answer.");
    }
  }';
header('Content-Type: text/javascript');

On client side

<script src="path/to/JS.php"></script>

Upvotes: 0

Obadah Hammoud
Obadah Hammoud

Reputation: 572

Yeah it's possible...let's say you have a mysql database for example...
You must put your link in it...for example look at this code that I've written for ya:

$query = "select * from lnkstabl";
$res = mysql_query($query);
$row = mysql_fetch_array($res);
echo '<a href="http://' . $row['link'] . '">go to link</a>';

Hope that I could solve your problem...

Upvotes: 0

Kvasir
Kvasir

Reputation: 1261

For do that you can can use javascript (jQuery : http://www.w3schools.com/jquery/jquery_hide_show.asp)

But this will not protect your webpage because people can always see the javascript. You had to protect your next page/level in php.

Upvotes: 0

Cristian Simionescu
Cristian Simionescu

Reputation: 80

You can try to start with a webpage that simply does not contain the next button/link then make it so that when they finish the "challenge" the webpage changes to the version that does have the Next Challenge link.

Upvotes: 0

Related Questions