October 23, 200719 yr It seems that I managed to stump everyone with my last question. I have a new problem... I've been requested to create a page that has access to several different forms. In order to access the form, the user will need to go through an "I Accept" page with the policy spelled out for them Click on "I Accept" Then be directed to the form QUESTION: Is there a way to make the form page so they cannot just type in the URL to get there, but make it so the are FORCED to only have access to the form page by going through the "I Accept" page? I've been searching and am not even sure what to look for to find the answer to this question. Please help if you can! Thanks so much. ~JJ
October 23, 200719 yr Hey newby. This is fairly straightforward - I'll assume you're using PHP, but it's going to be possible in any server-side language. You could even do it in Javascript, but that wouldn't be secure. Say for example that your policy page has the following: <form method="post" action="form.php"> <input type="submit" name="accept" value="I accept" /> </form> Clicking the submit button will take them to form.php. To check that they didn't just type the URL in, you'd have the following at the top of form.php: <?php if (!isset($_POST["accept"])) { // Do what you want here - display an error message, etc. // I'm going to send them back to the policy page header("Location: policy.php"); die; } ?>
October 23, 200719 yr Author Awesome Nathan, Thanks so much for your help!! I'll give it a try this week.
October 23, 200719 yr Just would like to confirm that Nathan's solution is spot on. I use this on many occassions to redirect users that have not gone through the 'correct' route to a page. My twist on the code is: <? // redirect to ....php if POST is NULL if ($_POST['accept'] == NULL) { header("Location: policy.php"); exit; } ?> But might start using Nathan's version of the code block as it looks more professional. And might be more efficient.
October 23, 200719 yr isset is better. if ($_POST['accept'] == NULL) will throw a notice in STRICT mode.
October 23, 200719 yr Changing my code to use 'isset' from now on. Am I also correct in reading that the redirect code block must be at the top of the page before any other characters, whitespace? I usually put some notes in a php code block for 'my eyes only' above the redirect code block and seems to be OK. So that must be characters, whitespace not in a php block that is not allowed, if you see what I mean.
October 24, 200719 yr Changing my code to use 'isset' from now on. Am I also correct in reading that the redirect code block must be at the top of the page before any other characters, whitespace? I usually put some notes in a php code block for 'my eyes only' above the redirect code block and seems to be OK. So that must be characters, whitespace not in a php block that is not allowed, if you see what I mean. Yes, all headers must be sent before anything is outputted to the browser. (any HTML, echo or print statement. (note that PHP errors outputted counts as well). You can use output buffering to catch any data that might be sent before the headers and ensure you send all your headers first. Have a look at http://no.php.net/manual/en/ref.outcontrol.php
October 24, 200719 yr Yep, that's right. The <? has to be the first thing in the page. No, the PHP opening tag doesn't have to be the first thing. It can appear anywhere. Might you be refering to the XML decleration?
Create an account or sign in to comment