Want user to agree to terms before granting access

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tcarp
Forum Newbie
Posts: 12
Joined: Tue Oct 25, 2011 9:09 pm

Want user to agree to terms before granting access

Post by tcarp »

I'm stuck figuring out how to have a user agree to some terms before granting access to parts of a site. I don't require registered users so there is no login.

Specfically, I have a page with some thumbs for maps. When the user navigates to that page I want to check to see if they have read and agreed to some terms and conditions. If not, I want to send them to a page where they can do so.

The authorization should last as long as their browser session lasts.

I've tried using header (location: ) on the map page to redirect to the t&c page after checking a global but when it redirects I get no css formatting. I also don't know how to get back to the maps page once the user accepts the t's and c's.

How do I do the check that the user has accepted the t's and c's? How do I redirect to the t's and c's page if they haven't? And how do I get back to the maps page once they accept?

Thanks
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Want user to agree to terms before granting access

Post by egg82 »

Code: Select all

if($_SESSION["agreed"] != true){
echo("You must agree to the terms to continue");
exit();
}

//page contents
just a thought
tcarp
Forum Newbie
Posts: 12
Joined: Tue Oct 25, 2011 9:09 pm

Re: Want user to agree to terms before granting access

Post by tcarp »

Thanks. Being new to php I need some additional help.

On the maps.php page I've put this code:

<?php
session_start();
if(isset($_SESSION["agree"]))
$readcaveat = 1;
else
header( 'location: ATL_counter_portal.php' );
?>

On the portal.php page (where the terms and conditions are, I've put this code:

<?php

if ($_POST)
$_SESSION["agree"] = "Yes";
header( 'location: ATL_counter_gallery.php' );

?>

The code on this page has a post form button "I agree".

From the maps.php page (where the user would start) I'm getting an error that the redirect isn't working properly.

Is the right code combination (maps.php and portal.php) to use?
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Want user to agree to terms before granting access

Post by egg82 »

instead of header, create an HTML form and use javascript to auto-submit it.
tcarp
Forum Newbie
Posts: 12
Joined: Tue Oct 25, 2011 9:09 pm

Re: Want user to agree to terms before granting access

Post by tcarp »

Unfortunately, I know less about javascript than I do about php.

If I could coax you into some more detail....

Currently there are two pages, gallery.php and portal.php. When the user goes to gallery I want to check that they've agreed to the terms so I redirect them to portal via the header. Do I still do that?

On portal, it sounds like instead of using header to check if the user has agreed, let the page execute and somehow have the form submit button "tell" gallery that the user has agreed. I've seen some code on other forums that seem to be showing autosubmit.

Is this an example?

<head>
<script language="JavaScript">
function MiFuncion(){location.href=document.MiFormulario.submit()};
</script>
</head>
<body>
<form action='http://www.MiPaginaWeb.com' name='MiFormulario'>
<input type='hidden' name='Nombre' value='Valor'>
</form>
<script>
MiFuncion();
</script>

Doing quite a bit of reading but still don't quite get how it will work in my scenario.

Sorry for being inexperienced with this stuff.
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Want user to agree to terms before granting access

Post by egg82 »

that's alright. Here's what I use (though, not being a javascript expert myself, I can't guarantee that it's "best practice", but it works fine)

Code: Select all

<?
echo('<form action="./" method="post" id="redir">');
?>
</form>
<script language="JavaScript" type="text/javascript">
<!--
document.getElementById('redir').submit();
//-->
</script>
<?
exit();
I think you might be having some header issues (header already sent) so that's why I recommended the form.
tcarp
Forum Newbie
Posts: 12
Joined: Tue Oct 25, 2011 9:09 pm

Re: Want user to agree to terms before granting access

Post by tcarp »

High marks for clairvoyance. I am getting header conflict messages.

Thanks for the code. I need to study it to make sure I understand where it goes (gallery or portal).

I've never used a url in the action portion of a form. I'm assuming portal has the form and needs to send the action to gallery to process.
tcarp
Forum Newbie
Posts: 12
Joined: Tue Oct 25, 2011 9:09 pm

Re: Want user to agree to terms before granting access

Post by tcarp »

This is failing. Left out any javascript thinking it might not be needed.

Clear cache and cookies before each test.

Either if (!isset $_SESSION['access']) is always TRUE or the Form in portal isn't sending control back to gallery. Test show $_Session['access'] has been successfully set. This implies Form.

See anything? New turf for me.

gallery.php
<?php // Confirm user agreement
if (!isset ($_SESSION['agree']))
{
header( "location: http://www.myspatialhome.org/ATL_counter_portal.php" );
}
$iagree = $_SESSION['agree']; // Testing
echo "Agree check shows $iagree"; // Testing
?>

portal.php
<?php
session_start();
$_SESSION["agree"] = "iagree";
$iagree = $_SESSION['agree']; // Testing
echo "I agree is set to: $iagree"; // Testing
?>

The form in portal.php
<form action="http://www.myspatialhome.org/ATL_counter_gallery.php" method="post" id="agree">
<input name='agree' value='I Agree' type='button'>
</form>
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Want user to agree to terms before granting access

Post by egg82 »

again, I would go with the javascript.

Put your session_start() in header.php and make both pages require() that file. That will (probably) destroy your header location, so again use the jscript and form
Post Reply