Jump to content

[SOLVED] need to restrict access to a page before first accepting terms & conditions by c


walrus111

Recommended Posts

Hi there. Does anyone know how I create a page that can't be accessed unless a checkbox has been checked stating that they have read the terms and conditions. I don't need any usernames , passwords, database etc so I thought it would be easy to find a tutorial somewhere but haven't had any luck. I really don't know much about PHP and was going to use javascript but that didn't help if javascript was disabled and also didn't stop people accessing the page directly. I guess I would need the user to be redirected back to the terms and conditions page if they hadn't been and agreed to the terms page first. Hope that is all the info someone needs to point me in the right direction for a tute or something. Any help much appreciated.

Link to comment
Share on other sites

it will enable submit button only if check box is checked...
<form action=""><tr ><td><input  type="checkbox" id="agree" name="agree" value="yes"   onClick="javascript:if(this.checked) 
this.form.submit.disabled=false; else this.form.submit.disabled=true;">I have read and accept the terms and conditions</td></tr>

<tr><td align=center colspan=2><input type="submit" id="submit" name="submit" value="Create Account"  onClick="return check();" disabled></td></tr></form>

Link to comment
Share on other sites

Sessions are an ok solution, but if the person has cookies turned off on their machine it wont work.

 

If you have no logins or anything like that, you can set it up this way not using sessions. First, you set up a form on the page with the terms and conditions. Include the checkbox in this form:

 

terms.php:

<form action="display_page.php" method="post">
<input type="checkbox" name="terms" />
<input type="submit" name="submit" />
</form>

 

Where display_page.php is the page that will have the information for which the person is agreeing to the terms and conditions.

 

Then, on display_page.php you set your code like this:

 

display_page.php

<?php
    if(!isset($_POST['terms'] || $_POST['terms'] != "checked" || $_SERVER['HTTP_REFERER'] != "terms.php")
    {
        header ("Location: terms.php");
    }
?>
// add your code here

 

This code has to be at the VERY top of your page, with no spaces or anything at all before it. If there is even a space before the opening php tag, it wont work.

 

What this does is check on the display page that the person has indeed come from the terms and conditions page, and that they have checked the box that says they agree to the terms and conditions. If they haven't, then it automatically forwards them to that page.

 

This will work in that there is no way to see the page without agreeing to the terms and conditions. But it wont keep any record of their having agreed to them, so even if someone were to break those terms and conditions, its possible that you may not have much legal recourse.

Link to comment
Share on other sites

Thanks Haku and good advice about the legal recourse too. I guess another notice on the "display page" as a further warning won't help. When I tested your script live I get this error Parse error: syntax error, unexpected T_BOOLEAN_OR, expecting ',' or ')' in /public/www/website/testingpage.php on line 2. I have checked that it is placed exactly as you have said with no spaces. Any ideas? Do I need to change the 'HTTP_REFERER' part?

Link to comment
Share on other sites

Just change

 

if(!isset($_POST['terms'] || $_POST['terms'] != "checked" || $_SERVER['HTTP_REFERER'] != "terms.php")

 

to

 

if(!isset($_POST['terms']) || $_POST['terms'] != "checked" || $_SERVER['HTTP_REFERER'] != "terms.php")

Was missing a ) here    ^

Link to comment
Share on other sites

Yeah, it just keeps forwarding back to the terms page named testingentrance.php??

 

testingentrance.php:

 

<head>

<title>Test</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link href="styles/text.css" rel="stylesheet" type="text/css">

</head>

<body>

<p> </p>

<div align="center">

<p>

<p><form action="testingpage.php" method="post">

<input type="checkbox" name="terms" />

<input type="submit" name="submit" />

</form>

</div>

</body>

</html>

 

 

 

testingpage.php:

 

 

if(!isset($_POST['terms']) || $_POST['terms'] != "checked" || $_SERVER['HTTP_REFERER'] != "testingentrance.php")

{

header ("Location: testingentrance.php");

}

?>

<html>

<head>

<title>Test</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link href="styles/text.css" rel="stylesheet" type="text/css">

</head>

<body>

<div align="center">

<p> </p>

<p><span class="text">Testing Page

</span>

</p>

</div>

</body>

</html>

 

Link to comment
Share on other sites

Now when I check it and hit submit the check disappears. Still stays on the same page. Are those last 2 .php pages at the end of the php script supposed to both be testingentrance.php? Does the form need to have a name? My 2 cents worth lol

Link to comment
Share on other sites

Try this:

 

<input type="checkbox" name="terms" />
if(!isset($_POST['terms']) || $_POST['terms'] != "on" || $_SERVER['HTTP_REFERER'] != "testingentrance.php")

 

(split the two lines between the two pages.

 

I think the problem is the value of the checkbox when its posted. I couldn't remember if it was checked, it looks like its not. I think it may be 'on' then.

 

If the code I am posting doesnt work, try deleting the three things in the 'if' statement one at a time, until the page doesnt redirect back to the original page. Then come back here and tell us which statement it was that caused that.

Link to comment
Share on other sites

Thanks for persevering with this guys. I tried deleting each of the 3 things one at a time but no luck but then I wasn't too sure how to seperate them to keep them still functioning?? The checkbox should be unchecked to start with so I have given it a value as "checked" when ticked. Another weird thing is that my submit button reads "Submit Query" although I only have a label as "Submit"??? 

Link to comment
Share on other sites

sessions are stored on the server. they have no coordination with the computer settings. So they can't turn them off.

 

 

also, these days, pretty much everyone has cookies enabled. They were first seen as a security risk, but now most major web sites use them.

Link to comment
Share on other sites

And the session ID is stored as a cookie on the users machine. That session ID is sent with the headers to the server. If the user has cookies turned off, the cookie with the session ID isn't stored on the users computer, which means the ID isn't sent with the headers, which means that the server has no way of connecting the user with the session.

 

ALTHOUGH, there is a way around this - sending the session ID in the URL. But this isnt the most secure way.

 

A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

php.net

 

also, these days, pretty much everyone has cookies enabled. They were first seen as a security risk, but now most major web sites use them.

 

While this is true, the fact that users can turn them off with a simple browser setting means that its not a reliable method. As such, its better to use a more reliable method if one exists.

Link to comment
Share on other sites

Ok, I tried and tested the code. Sorry, I didn't have time until now. The problem was that in the code I gave, I was testing the referring page against the page name only, instead of the absolute link. Anyways, here is the working code:

 

first_page.php

<form action="second_page.php" method="post">
<input type="checkbox" name="terms" value="agree" />
<input type="submit" name="submit" />
</form>

 

second_page.php

<?php
if(!isset($_POST['terms']) || $_POST['terms'] != "agree" || $_SERVER['HTTP_REFERER'] != "http://www.sitename.com/first_page.php")
{
	header ("Location: first_page.php");
}
?>

 

You will have to adjust the script by changing the following three values:

 

1) first_page.php - make this the page name of the first page. As long as they are in the same folder, you can use the page name only, and it can be any extension.

2) second_page.php - make this the name of the second page. Again, as long as they are in the same folder, you can just include the page name. This HAS to have a file extension of PHP (unless your server is configured to parce non-php files as php).

3) http://www.sitename.com/first_page.php - you have to change this to the absolute address of the first page. You must include the entire address including the http://

Link to comment
Share on other sites

i don't use session id's. just use something like

 

$_SESSION['clicked'] = 'yes';

 

Not using session IDs is not something you have a choice on when you are using sessions. If you are using sessions, you are using session IDs. When you call session_start(), it creates a session ID whether you like it or not. And that session ID is stored on the users computer. You cant use sessions without calling session start, and as a result if you are using sessions you are also using session IDs.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.