Hello I wanted to know how I can make a protect a page and redirect to other pages after a form submission like. http://ranksurveys.com/users/signup they are doing here after u submitted the form u can not access that page again.. and you will auto redirected to http://ranksurveys.com/surveys I am really curious hope someone can help me!
You can use cookies. Whether it can be use as server side or client side. This can be easily approach with Jquery cookies.
step 1 : add script files
<head>
<script src="path/to/jquery.js"></script>
<script src="path/to/jquery.cookie.js"></script>
</head>
step 2: in the protected form page add this script
var isProtected = $.cookie('page-protected');
if(isProtected == 'YES'){
// We dont need to stay them on current page if they have already submitted the form
window.location = "http://yoursite.com/your-thanking-page";
}
$.cookie('page-protected', 'NO');
step 3: in the thanking page you should set the cookie YES if the relevant form submitted successfully.
if(formSubmitComplete){
$.cookie('page-protected', 'YES');
}
When user submits the form you just put information that form were submitted into the cookies session. Before rendering the form you just check the values in cookie\session and if you found that the form were already submitted just send Location and redirect status code via PHP. You should avoid implementing this in JS because anyone can easily disable Javascript in the browser.
Here is some ugly but fast code exmaples just to explain the concept 1. Retrieving the form
/* These are our valid username and passwords */
$user = 'jonny4';
$pass = 'delafoo';
if (isset($_POST['username']) && isset($_POST['password')) {
if (($_POST['username'] == $user) && ($_POST['password'] == $pass)) {
if (isset($_POST['rememberme'])) {
/* Set cookie to last 1 year */
setcookie('username', $_POST['username'], time()+60*60*24*365, '/account', 'www.example.com');
setcookie('password', md5($_POST['password']), time()+60*60*24*365, '/account', 'www.example.com');
} else {
/* Cookie expires when browser closes */
setcookie('username', $_POST['username'], false, '/account', 'www.example.com');
setcookie('password', md5($_POST['password']), false, '/account', 'www.example.com');
}
header('Location: index.php');
} else {
echo 'Username/Password Invalid';
}
} else {
echo 'You must supply a username and password.';
}
Access cookie and redirect to login if not logged in. I hope you'll understand how to implement an opposite case ;)
/* These are our valid username and passwords */ $user = 'jonny4'; $pass = 'delafoo';
if (isset($_COOKIE[['username']) && isset($_COOKIE['password')) {
if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass))) {
header('Location: login.html');
} else {
echo 'Welcome back ' . $_COOKIE['username'];
}
} else { header('Location: login.html'); }