How to increment number from previous number in PHP - php

I want to generate a "number in sequence" in PHP on page refresh. Like
if I start with "1" then after page refresh, it should be "2". Please help.

You can achieve this using PHP SESSION
$_SESSION['number']++ will do the trick
PHP
<?php
session_start();
if (!isset($_SESSION['number'])) {
$_SESSION['number'] = 0;
}
$_SESSION['number']++;
?>

Related

change random number when echo

i have a problem with rand function in php. i set random number on a $_SESSION and in other place echo this. but every time i echo $_SESSION, value on it is changed.
my code in page1:
session_start();
$_SESSION['y'] = rand(1,100);
echo $_SESSION['y'];
and in other page2 i write this:
session_start();
echo $_SESSION['y'];
how can i solve it?
note that page2 is appended with ajax to page1 when clicking on a button.
It sounds like you are including page 1 on page 2.
Option 1
Do not include page 1 on page 2.
Option 2
Check to see if the random number exists before setting it.
session_start();
if (! isset($_SESSION['y'])) {
$_SESSION['y'] = rand(1,100);
}
echo $_SESSION['y'];

how can i track last 5 pages visited using cookies in php

<?php
session_start();
if(empty($_SESSION['track']))
$_SESSION['history'] = array($_SERVER['PHP_SELF']);
else {
$_SESSION['track'][] = $_SERVER['PHP_SELF'];
if(count($_SESSION['track']) > 5)
array_shift($_SESSION['track']);
}
function display()
{
print_r(array_values($_SESSION['track']));
}
?>
i was able to do it using session but i need to use only cookies and php to track the last 5 pages visited.
any idea guys??
Any comment or answer will appreciate. Thanking in advance.
$url = unserialize($_COOKIE['history']);
array_push($url,your_url);
setcookie('history', serialize($url));
onload of every page first retrieve the value of cookie in url and push the current url in it and add set it to cookie

PHP- Value of SESSION variable not echoing properly when page is refreshed

I have a session variable that is updated when a link is clicked which adds GET variables to the current URL:
<?php
$_SESSION['test'] = 0;
if (isset($_GET['do'] && $_GET['do'] == 'this')) {
$_SESSION['test'] = $_SESSION['test'] + 1;
}
echo $_SESSION['test'];
?>
Click me
When the link is clicked, the new session value that is echoed is still 0.
Why? How do I get this to echo the updated value? (1)
EDIT: Yes, session_start() is included at the top of the page.
change it to be like this:
<?php
session_start();
if(!array_key_exists('test', $_SESSION)){
$_SESSION['test'] = 0;
}
if(array_key_exists('do', $_GET) && $_GET['do'] === 'this') {
$_SESSION['test']++;
}
echo $_SESSION['test'];
?>
Click me
I like array_key_exists() more than isset because the key can exist being null and still pass the test:
From the php docs: "isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does."
You also were using isset incorrectly to test the contents of $_GET['do']. To top things off, you didn't start the session on each page load.
session_start() will initialize the $_SESSION variable and fill it with the appropriate contents. Since this was not in your code, the $_SESSION variable never had your 'do' key.
The second-to-last problem you had was the very first line, you were always setting the 'test' key to 0 causing the outcome to always be 1 after clicking Click me
and finally, you didn't close the php tag prior to outputting raw HTML which should have given you an error
Maybe you have not paste the whole code but here is how it should have been done.
<?php
session_start();
$_SESSION['test'] = 0;
if (isset($_GET['do']) && $_GET['do'] == 'this') {
$_SESSION['test'] += 1;
}
echo $_SESSION['test'];
?>
Click me
You need to start with session_start() Google to learn more about it.
You have put everything in the isset() Google isset() to learn more about it.
you need to close php tags before you can insert raw HTML. Inside HTML you can execute php with <?php ?> tags.
Hope this helps.
As mentioned before, you have everything wrapped in isset() should be
isset($_GET['do'])
not
isset($_GET['do']
Also, you're session is always being set to 0 on each page refresh. You need to only set $_SESSION['test'] = 0 if $_SESSION['test'] has not been set yet.
if (!isset($_SESSION['test'])) {
$_SESSION['test'] = 0;
}
You are missing session_start() at the top of your script. You're also not assigning the session if it doesn't previously exist, you're continuously setting it to 0 before incrementing.

Last 5 Page Names User Viewed

I'm trying to code the last 5 page names the user viewed on my site and produce it into a list. I'm currently able to get the current page name, but I don't know how to get the previous pages. This is the code I'm using to get the current page name:
<?php
$pageName = basename($_SERVER['PHP_SELF']);
echo $pageName;
?>
PHP Sessions should get you going in the right direction. For example:
session_start();
if(!isset($_SESSION['pages'])) {
$_SESSION['pages'] = array();
}
if(count($_SESSION['pages']) < 5) {
$_SESSION['pages'] [] = $_SERVER['PHP_SELF'];
} else {
echo "Limit reached";
}
print_r($_SESSION['pages']);
I recommend you use PHP Sessions to accomplish this.
So, save the current page name that you want to the sessions variable like so:
<?php
$pageName = basename($_SERVER['PHP_SELF']);
$_SESSION['pageName'] = $pageName;
?>
And then continue to save these names. #Len_D just beat me to the punch with an answer that uses arrays and is likely what you need.

open random php pages from array list when I click next

I've hit a dead end. I've been working on this for 3 weeks with no result.
I have 10 php pages (1.php, 2.php, ..., 10.php) and a starting page (start.php).
All I want to do is randomize the 10 pages with no repeat, so when I click "next" in the start.php it should go to one of the 10 pages (let's say for example it goes to 4.php). When I click "next" in 4.php it should redirect to another within the 10 pages except for 4.php.
It should continue this until all the numbers (1.php - 10.php) have been displayed. At this point it should randomize again. When I click "next" in the last number .php displayed, it should randomize the number and go back to the first on the random list.
Here's what I have so far:
start.php:
<?php $vidcount = 1; ?>
<? include ("source.php"); ?>
next page
source.php:
<?php
include ("start.php");
$numbers = range(1, $total_songs);
if(($vidcount == $total_songs)||($vidcount == 1)){
shuffle($numbers);
$vidcount = 1;
}
$nextvid[1] = $numbers[0];
$nextvid[2] = $numbers[1];
$nextvid[3] = $numbers[2];
$nextvid[4] = $numbers[3];
$nextvid[5] = $numbers[4];
$nextvid[6] = $numbers[5];
$nextvid[7] = $numbers[6];
$nextvid[8] = $numbers[7];
$nextvid[9] = $numbers[8];
$nextvid[10] = $numbers[9];
?>
1.php, 2.php, ... 10.php:
<?php
include("source.php");?>
<?php echo $vidcount; ?>
next page
<?php $vidcount++;?>
1.php - 10.php have the same code. I also have a source.php which is supposed to keep track of what number has been displayed and re-shuffle when all the numbers have been displayed.
Please help. I'll greatly appreciate any help I can get.
You don't have to use the above code, I don't mind starting from scratch if you have a different idea as long as the code I get works.
Well firstly why do you have ten files when you could just have one file and ?id=X in the URL? But never mind that.
Your best bet is to use a session variable. Something like this:
<?php
session_start();
if( !isset($_SESSION['sequence']) || !$_SESSION['sequence']) {
$_SESSION['sequence'] = shuffle(range(1,10));
}
$next = array_shift($_SESSION['sequence']);
// now use $next to create your "Next page" link.
?>

Categories