You're on the right track, but looking at your code I guess that your variable $pagenum
does not exist. The $_SESSION is just like an array, so use an unique string to assign something, e.g. $_SESSION['pageNumber']
. Also in this case you cannot miss the else, or the starting value will be 2. So your code would become:
if(!isset($_SESSION['pageNumber'])) {
$_SESSION['pageNumber'] = 1;
} else {
$_SESSION['pageNumber']++;
}
In your HTML you can echo it with:
<h1>Hello world!</h1>
<p>You are visiting page <?php echo $_SESSION['pageNumber']; ?></p>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…