How to use conditional statements to set cookie based on input from HTML form?

Multi tool use
I'm setting up a website that will display posts based on user input instead of using geolocation the website owner want's visitors to choose their location manually.
To achieve this I have made a conditional statement to check for if the cookie
is already there, and if not, a popup will display with a form that contains buttons that the user can click to choose their "location." The form is then sent to the template page and based on an "else if" statement and the correct cookie value is set.
The problem with this approach
1. Using this method, the form ends up on the template that holds the "$_GET" for the form inputs. I want to redirect it to the front page instead (Home).
2. The cookie that is set disappears as soon as the visitor goes to another page on the site.
3. I find this method ($_GET) to be very "open" and exploitable for many attacks.
What do I need to add to redirect to the home page after PHP has processed the form, and so that the cookie doesn't disappear when browsing the site?
Some simple security measures would also be much appreciated.
There is a lot of "elseif" conditionals in the original code, but they're all the same in principle, the only difference is, the values they send, receive and set. The code is the same. That's why I don't include every conditional statement here.
The code for setting the cookie and checking if the cookie exists. This code is located in the functions.php file.
<?php
//Set Cookie akr
function set_mycookie_cookie() {
$_a = 'akr';
if(!isset($_COOKIE['tln_c_no'])) {
// set a cookie for 1 year
setcookie('tln_c_no', $_a, time()+31556926, '/');
///Cookie path has also been set to "/" instead of '/', but it makes no difference.
}
}
//Check for cookies function////////
function check_for_cookies() {
// Check if cookie is already set
if(isset($_COOKIE['tln_c_no'])) {
// Do this if cookie is set
?>
<script>
alert("Hi, and welcome back!");
</script>
<?php
} else {
// Do this if the cookie doesn't exist
echo do_shortcode("[sg_popup id=1]");
}
}
add_action('wp_head', 'check_for_cookies');
?>
The HTML form (Located on the same dedicated custom-template.php as the $_GET)
<form action="/custom-template.php" method="get" target="_self">
<input type="submit" name="fylket_a" value="AKR" />
</form>
The PHP $_GET code (Located in the same template as the HTML form)
if($_GET){
if(isset($_GET['fylke_a'])){
set_akr_cookie();
}elseif(isset($_GET['fylke_b'])){
set_akb_cookie();
}else{
echo "Ooops!";
}
}
php wordpress cookies get html-form
New contributor
Steve Rodgers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I'm setting up a website that will display posts based on user input instead of using geolocation the website owner want's visitors to choose their location manually.
To achieve this I have made a conditional statement to check for if the cookie
is already there, and if not, a popup will display with a form that contains buttons that the user can click to choose their "location." The form is then sent to the template page and based on an "else if" statement and the correct cookie value is set.
The problem with this approach
1. Using this method, the form ends up on the template that holds the "$_GET" for the form inputs. I want to redirect it to the front page instead (Home).
2. The cookie that is set disappears as soon as the visitor goes to another page on the site.
3. I find this method ($_GET) to be very "open" and exploitable for many attacks.
What do I need to add to redirect to the home page after PHP has processed the form, and so that the cookie doesn't disappear when browsing the site?
Some simple security measures would also be much appreciated.
There is a lot of "elseif" conditionals in the original code, but they're all the same in principle, the only difference is, the values they send, receive and set. The code is the same. That's why I don't include every conditional statement here.
The code for setting the cookie and checking if the cookie exists. This code is located in the functions.php file.
<?php
//Set Cookie akr
function set_mycookie_cookie() {
$_a = 'akr';
if(!isset($_COOKIE['tln_c_no'])) {
// set a cookie for 1 year
setcookie('tln_c_no', $_a, time()+31556926, '/');
///Cookie path has also been set to "/" instead of '/', but it makes no difference.
}
}
//Check for cookies function////////
function check_for_cookies() {
// Check if cookie is already set
if(isset($_COOKIE['tln_c_no'])) {
// Do this if cookie is set
?>
<script>
alert("Hi, and welcome back!");
</script>
<?php
} else {
// Do this if the cookie doesn't exist
echo do_shortcode("[sg_popup id=1]");
}
}
add_action('wp_head', 'check_for_cookies');
?>
The HTML form (Located on the same dedicated custom-template.php as the $_GET)
<form action="/custom-template.php" method="get" target="_self">
<input type="submit" name="fylket_a" value="AKR" />
</form>
The PHP $_GET code (Located in the same template as the HTML form)
if($_GET){
if(isset($_GET['fylke_a'])){
set_akr_cookie();
}elseif(isset($_GET['fylke_b'])){
set_akb_cookie();
}else{
echo "Ooops!";
}
}
php wordpress cookies get html-form
New contributor
Steve Rodgers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I'm setting up a website that will display posts based on user input instead of using geolocation the website owner want's visitors to choose their location manually.
To achieve this I have made a conditional statement to check for if the cookie
is already there, and if not, a popup will display with a form that contains buttons that the user can click to choose their "location." The form is then sent to the template page and based on an "else if" statement and the correct cookie value is set.
The problem with this approach
1. Using this method, the form ends up on the template that holds the "$_GET" for the form inputs. I want to redirect it to the front page instead (Home).
2. The cookie that is set disappears as soon as the visitor goes to another page on the site.
3. I find this method ($_GET) to be very "open" and exploitable for many attacks.
What do I need to add to redirect to the home page after PHP has processed the form, and so that the cookie doesn't disappear when browsing the site?
Some simple security measures would also be much appreciated.
There is a lot of "elseif" conditionals in the original code, but they're all the same in principle, the only difference is, the values they send, receive and set. The code is the same. That's why I don't include every conditional statement here.
The code for setting the cookie and checking if the cookie exists. This code is located in the functions.php file.
<?php
//Set Cookie akr
function set_mycookie_cookie() {
$_a = 'akr';
if(!isset($_COOKIE['tln_c_no'])) {
// set a cookie for 1 year
setcookie('tln_c_no', $_a, time()+31556926, '/');
///Cookie path has also been set to "/" instead of '/', but it makes no difference.
}
}
//Check for cookies function////////
function check_for_cookies() {
// Check if cookie is already set
if(isset($_COOKIE['tln_c_no'])) {
// Do this if cookie is set
?>
<script>
alert("Hi, and welcome back!");
</script>
<?php
} else {
// Do this if the cookie doesn't exist
echo do_shortcode("[sg_popup id=1]");
}
}
add_action('wp_head', 'check_for_cookies');
?>
The HTML form (Located on the same dedicated custom-template.php as the $_GET)
<form action="/custom-template.php" method="get" target="_self">
<input type="submit" name="fylket_a" value="AKR" />
</form>
The PHP $_GET code (Located in the same template as the HTML form)
if($_GET){
if(isset($_GET['fylke_a'])){
set_akr_cookie();
}elseif(isset($_GET['fylke_b'])){
set_akb_cookie();
}else{
echo "Ooops!";
}
}
php wordpress cookies get html-form
New contributor
Steve Rodgers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'm setting up a website that will display posts based on user input instead of using geolocation the website owner want's visitors to choose their location manually.
To achieve this I have made a conditional statement to check for if the cookie
is already there, and if not, a popup will display with a form that contains buttons that the user can click to choose their "location." The form is then sent to the template page and based on an "else if" statement and the correct cookie value is set.
The problem with this approach
1. Using this method, the form ends up on the template that holds the "$_GET" for the form inputs. I want to redirect it to the front page instead (Home).
2. The cookie that is set disappears as soon as the visitor goes to another page on the site.
3. I find this method ($_GET) to be very "open" and exploitable for many attacks.
What do I need to add to redirect to the home page after PHP has processed the form, and so that the cookie doesn't disappear when browsing the site?
Some simple security measures would also be much appreciated.
There is a lot of "elseif" conditionals in the original code, but they're all the same in principle, the only difference is, the values they send, receive and set. The code is the same. That's why I don't include every conditional statement here.
The code for setting the cookie and checking if the cookie exists. This code is located in the functions.php file.
<?php
//Set Cookie akr
function set_mycookie_cookie() {
$_a = 'akr';
if(!isset($_COOKIE['tln_c_no'])) {
// set a cookie for 1 year
setcookie('tln_c_no', $_a, time()+31556926, '/');
///Cookie path has also been set to "/" instead of '/', but it makes no difference.
}
}
//Check for cookies function////////
function check_for_cookies() {
// Check if cookie is already set
if(isset($_COOKIE['tln_c_no'])) {
// Do this if cookie is set
?>
<script>
alert("Hi, and welcome back!");
</script>
<?php
} else {
// Do this if the cookie doesn't exist
echo do_shortcode("[sg_popup id=1]");
}
}
add_action('wp_head', 'check_for_cookies');
?>
The HTML form (Located on the same dedicated custom-template.php as the $_GET)
<form action="/custom-template.php" method="get" target="_self">
<input type="submit" name="fylket_a" value="AKR" />
</form>
The PHP $_GET code (Located in the same template as the HTML form)
if($_GET){
if(isset($_GET['fylke_a'])){
set_akr_cookie();
}elseif(isset($_GET['fylke_b'])){
set_akb_cookie();
}else{
echo "Ooops!";
}
}
php wordpress cookies get html-form
php wordpress cookies get html-form
New contributor
Steve Rodgers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Steve Rodgers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Dec 27 '18 at 16:15


Funk Forty Niner
80.5k1247101
80.5k1247101
New contributor
Steve Rodgers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Dec 26 '18 at 10:58
Steve Rodgers
95
95
New contributor
Steve Rodgers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Steve Rodgers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Steve Rodgers is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Steve Rodgers is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53931121%2fhow-to-use-conditional-statements-to-set-cookie-based-on-input-from-html-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Steve Rodgers is a new contributor. Be nice, and check out our Code of Conduct.
Steve Rodgers is a new contributor. Be nice, and check out our Code of Conduct.
Steve Rodgers is a new contributor. Be nice, and check out our Code of Conduct.
Steve Rodgers is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53931121%2fhow-to-use-conditional-statements-to-set-cookie-based-on-input-from-html-form%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
lv 2TushHoavJeTPfb1SHhcUS6Ua9VflF2XNwonokHtPo5tU9kbDY4R,55ahH,q9,Ah2L