Question: In my page, I'm using a javascript function

```
<script> 
function redirect(){ 
    window.location="hurray.php";
}
</script>

```

Calling the function from the line below.

```
<input id="search_box" name="textbox" type="text" size="50" maxlength="100" onkeypress="redirect()" />

```

Now I want to make it sure that the page 'hurray.php' is visited only from this action. If I typed the direct URL to 'hurray' page, I should not be able to visit this page, rather redirect it to this previous page.

Answer: 
Make an AJAX call to a PHP function that will set a variable in the session. When the AJAX call returns response redirect the user to this page and check for the session variable. You can delete it if you do not want the user to be able to visit it again for this session.
You cannot do this using javascript alone, I don't think.

You need to intercept this on the server and handle it accordingly.

Your probably going to need a token to be sent along with the redirect, you can then validate this token server side and allow the redirect to complete or do some other action if the user has been sent there in error or by typing in the URL directly.

Why are you wanting to do this in the example you give? Surely this would lead the user away from the search form and to another page?