<?php
session_start();
if(isset($_POST['sessionNum'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
$_SESSION['sessionNum'] = $_POST['sessionNum'];
}
if(!isset($_SESSION['sessionCount'])){
$_SESSION['sessionCount'] = 1;
}
else
{
$_SESSION['sessionCount']++;
}
$sessionMinus = $_SESSION['sessionCount'];
?>
How do I get it so that if $_SESSION['sessionCount'] is less than $_SESSION['sessionNum'], then add 1 to $_SESSION['sessionCount'] and if it equals $_SESSION['sessionNum'], then stop adding 1 to $_SESSION['sessionCount']?
Also if I go back on a previous page and I go back onto this page, I want $sessionMinus to go back to '1', and finally if the user refreshes the page, then whatever number $sessionMinus is, keep it on that number when page refreshes.
How do I get it so that if $_SESSION['sessionCount'] is less than
$_SESSION['sessionNum'], then add 1 to $_SESSION['sessionCount'] and
if it equals $_SESSION['sessionNum'], then stop adding 1 to
$_SESSION['sessionCount']?
if (!isset($_SESSION['sessionCount'])) {
$_SESSION['sessionCount'] = 1;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
++$_SESSION['sessionCount'];
}
Also if I go back on a previous page and I go back onto this page, I
want $sessionMinus to go back to '1'
To do that you have to set $_SESSION['sessionMinus'] (or some other variable) in the previous page. Once this page is reached, the only way to know what happened earlier is specifically through $_SESSION variables. You cannot detect it on the spot.
and finally if the user
refreshes the page, then whatever number $sessionMinus is, keep it on
that number when page refreshes.
This is not possible. You cannot tell if the page was refreshed or loaded from scratch¹. What you could do is use the PRG pattern and count the "P" page as "the user just got here" and the "G" page as "the user has refreshed the page". You can set a variable (e.g. $_SESSION['redirecting'] = true) from the "P" page and modify it on the "G" page ($_SESSION['redirecting'] = false); just before you do that, check if it was true to begin with. If it was, then the user is here due to your redirect (which will only happen once). If it was already false, they have refreshed the page.
¹You can try to do it, again through $_SESSION, but really you are just guessing. There is no way to know for certain.
if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
// sessionNum is bigger then sessionCount
$_SESSION['sessionCount']++;
}
Related
I've created this code so that the user will not redirect onto the next page again. I set the maximum value on votenow button to 1 and once the user click the button again the value on the votenow button will not be added anymore since its maximum is 1. Everything works fine except that the user can still access the nextpage though the maximum value on the button was reached. Here's the code.
<?php
$errors = array();
$db = mysqli_connect("localhost","root","","registration");
if(isset($_POST['votenow']))
{
$votenow ="0";
$votenow1="1";
if($votenow != $votenow1){
$votenow = "update users set votenow = votenow + 1 WHERE votenow=0 LIMIT 1 ";
$run_vote = mysqli_query($db,$votenow);
echo '<script type="text/javascript">alert("hello!");</script>';
header ("Refresh:2; url=renewsys3.php");
}
else{
echo '<script type="text/javascript">alert("oh not again!");</script>';
header ("Refresh:2; url=renewsys2re.php");
}
}
?>
Can you guys help me or is there any other way so that the user will not enter the next page again? Btw, this is a voting system
The best way to use session:
$_SESSION['votenow'] = 0;
if (isset($_SESSION['votenow'])) {
if (!$_SESSION['votenow']) {
//do something
} else { // means $_SESSION['votenow'] == 1
// do something else
}
}
Redirect the URL to a different page (maybe homepage or any page) If the maximum vote is reached.
Let that code run before any other code so that the page won't load of the condition is true. It'll redirect immediately.
In this code I'm trying to ban client if he/she/it doing to much(10) login request for 3 minutes. The problem is after 3 minutes user must refresh the page 2 times. I can see the reason why it's enter into if statement but I can't find the solution. I feel like I've overcoded.
if($this->sessions->get_data("wrong_login")>10){
if(!isset($_SESSION["ban_time"])){
$this->sessions->set_data("ban_time", time());
}else
{
if(time() - $this->sessions->get_data("ban_time") > 180){ // 180 seconds
$this->sessions->remove("ban_time");
$this->sessions->remove("wrong_login");
}
}
// The message if user still banned
die("Banned for 3 minutes!");
}
I hope I can tell the problem..
EDIT: This code is the inside of the construct of register controller.
Before your IF statement, add another if statement that checks for ban_time session if the time is up, then set the wrong_login session to 0 if it is.
if($this->sessions->get_data("ban_time") < time())
{
$this->sessions->remove("ban_time");
$this->sessions->set_data("wrong_login", 0);
}
remove your else statement there.
also forgot to mention! when you set the ban time, it should be time() + 180.
if(!isset($_SESSION["ban_time"])){
$this->sessions->set_data("ban_time", time()+180);
}
use header function.
e.g.
header("Location: /path/to/some/file.php?error=Banned for 3 minutes.");
Then on the file.php you can do this:
<?php
// Parse error
$error = isset($_GET['error']) ? $_GET['error'] : '';
// Display error (if any) and stop executing the rest of the code.
if (!empty($error)) {
exit($error);
}
?>
This will not work if you already started to output...
I have a problem when it comes to displaying the correct number in my heading. When a user submits a page and navigates to this current page, I want the heading below to appear:
CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 1 OF 3
As you can see it above it starts with the number 1 as the user is creating their first session. The problem I am getting is that it never displays number 1, it just keeps displaying this below:
CREATING QUESTIONS AND ANSWERS: SESSION (AAA) 3 OF 3
It keeps displaying the number 3 which is incorrect as if the user enters the current page for the first time then obviousl they don't start with session 3, they start with session 1, then 2 then 3.
So my question is how do I get "SESSION 1" to be displayed when the useer enters the page for the first time?
Below is the current code I have:
if(isset($_POST['sessionNum'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
$_SESSION['sessionNum'] = $_POST['sessionNum'];
}
if (!isset($_SESSION['sessionCount'])) {
$_SESSION['sessionCount'] = 1;
}
else if ($_SESSION['sessionCount'] < $_SESSION['sessionNum']) {
++$_SESSION['sessionCount'];
}
$sessionMinus = $_SESSION['sessionCount'];
...
<h1>CREATING QUESTIONS AND ANSWERS: SESSION (<?php echo $_SESSION['id'] ?>) <?php echo $sessionMinus ?> OF <?php echo $_SESSION['initial_count'] ?></h1>
When you are setting for the first time:
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['sessionNum'];
Set it to 1! Because after that isset will return true, and so, it will not be set to 1...
//Declare my counter for the first time
$_SESSION['initial_count'] = 1;
I have some variable where everytime the form is submiited or page is refreshed, it adds 1 to $sessionMinus. But if the user goes onto the previous page and then goes back onto this page, I want $sessionMinus to go back to being '1'.
At the moment if lets say the number is 3 and then the user goes back to previous page and then back to this page, it still displays 3, but I want it to go back to 1. I heard I have to set either $sessionMinus or $_SESSION['sessionCount'] to '1' on the previous page but how do I do this?
below is the code for the current page (not previous page) on how the $sessionMinus is declared and incremented:
if(!isset($_SESSION['sessionCount'])){
$_SESSION['sessionCount'] = 1;
}
else
{
$_SESSION['sessionCount']++;
}
$sessionMinus = $_SESSION['sessionCount'];
?>
On the previous page, do:
if( isset( $_SESSION['sessionCount'])){
$_SESSION['sessionCount'] = 1;
// Or maybe 0 if you want $sessionMinus = 1 on next page
}
I have 3 pages that I am trying to "track" so to speak. The signup process is 3 pages. Basically, I'm not wanting to count hits that are out of sequence (ie. if the user checks back into the thank you page (3rd page)) I don't want it to count a conversion again.
So something like this
1. User enters landing page +1 hit
2. User enters intermediary page +1hit
3. User enters thank you page +1 conversion
I know how to increment the count pretty easy for hits, but am unsure how to "not" count it if they are out of sequence.
You would set up a session variable so that on page 1 it was set to '1', page 2 = '2' and so on, but only set the variable if it is currently less then the current page number, so:
session_start();
if($_SESSION['cur-page'] < [CURRENT PAGE NUMBER]) {
$_SESSION['cur-page'] = [CURRENT PAGE NUMBER];
logHit();
}
Where logHit() tracks the page hit.
SESSIONS will solve your problem here.
On the first page do something such as:
session_start();
$_SESSION['page'] = '1';
// Store signup values in session
On the second:
session_start();
if ($_SESSION['page'] != 1 || $_SESSION['page'] != 3) {
header("Location: /page1");
exit();
} elseif ($_SESSION['page'] == 'complete') {
header("Location: /resubmit-error");
} else {
$_SESSION['page'] = 2;
}
// Store additional values in session
On the third:
session_start();
if ($_SESSION['page'] != 2) {
header("Location: /page2");
exit();
} elseif ($_SESSION['page'] == 'complete') {
header("Location: /resubmit-error");
} else {
$_SESSION['page'] = 3;
}
// Store additional values in session again and submit values to database or other source.
And finally the thank you page:
if ($_SESSION['page'] != 3) {
header("Location: /page3");
} elseif ($_SESSION['page'] == 'complete') {
header("Location: /resubmit-error");
} else {
$_SESSION['page'] = 'complete';
// Store data
}
On final submission of the third page redirect to completion / thank you page.
With this method a user wouldn't simply be able to return to the final page without re-completing the form.
Set a cookie or session variable called step. Before you run the tracking code, make sure the current step is greater than the step stored in the cookie, else don't track it. Usually for conversion tracking, it's desirable to use cookies over a session because you can track conversions for 30 days (or however long you want), rather than a short session which usually ends when a user closes the browser window.
On Page 1
$step=1;
if (!$_COOKIE['step']) {
setcookie('step',$step,time()+(86400*30),'/','.yourdomain.com');
//run tracking code (log database, whatever)
}
On Page 2+
$step=2;
if ($_COOKIE['step'] && $_COOKIE['step']<$step) {
setcookie('step',$step,time()+(86400*30),'/','.yourdomain.com');
//run tracking code (log database, whatever)
}