how to set variable on previous page back to 1 - php

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
}

Related

Prevent user from entering page again after logout

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.

how to navigate user to correct page?

<?php
$pages = array('Text1.php', 'Text2.php', 'Text3.php', 'Text4.php', 'Text5.php');
// Track $latest in either a session variable
// $current will be dependent upon the page you're on
$latest = $_SESSION['latest'];
$current = basename(__FILE__);
$currentPages = array_search($current, $pages);
$latestPages = array_search($latest, $pages);
if ($currentPages - $latestPages > 1 ) {
?>
<div class="boxed">
Continue
<br/>
Create New
</div>
<?
} else {
// let user do their step
}
?>
I have an array which contains five pages. Now this page steps.php is externalized and stored in an an include() which is stored in 5 php pages, the same php pages stored in the array above in the array.
Now what I am trying to do is the user is suppose to follow the page structure. So if the user is on one of the pages in the array, they cannot access another page until they have submitted the page they are currently on.
But my question is that if the user click on the Continue link, how can I get the link </a> to link to the correct page so it navigates the user to the page they should be correctly on.
If you mean how you can direct the user to the correct 'next' page when they click on Continue, you can output the next page using the currentPages index + 1
<? if ($currentPages - $latestPages > 1 ) { ?>
<div class="boxed">
Continue
<br/>
Create New
</div>
<? } ?>
As much as I understand, you are trying to implement something like pagination? You should put current, previous, and next pages in the SESSION variable, so in every request you can check whether the user is in the SESSION['current'] page, and tries to go to the SESSION['previous'] or the SESSION['next'] page, also you should send a 'hiddenfield' value to the server in order to check if the user 'submitted' the page(but of course, one can simply read your html and find the 'hidden' field). Or you can simply check whether the submit button's 'name' is in the POST(or GET)? (if($_POST['NAME_OF_SUBMIT']){}) - but again it is simple to falsify, too.

how to display the number 1 each time user accesses the page

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;

How to stop adding a number when it reaches its limit

<?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']++;
}

PHP Conversion Tracking

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)
}

Categories