如何使用php在第一页上增加会话值一


How can i increment session value one on first page using php?

我需要从一开始增加会话值,现在只能从两开始增加。如何从第一页开始递增?

事实上,我正试图在pdf页面中增加收据编号。

有人能帮我解决这个问题吗?

这是我的代码:

<?php
    session_start();
    //$_SESSION['rid']=0;
    if(!empty($_POST['register']))
    {   
        $_SESSION['rid'] = $_SESSION['rid']+ 1;
require("fpdf/fpdf.php");
    $pdf=new FPDF();
    $pdf->Addpage();
$pdf->SetFont("Arial","B",18);
    $pdf->Cell(0,10,"Receipt number:".$_SESSION['rid'],0,0,'C');
    $pdf->output();
    }
?>

ooh我现在有你的问题了。你可以这样做:

<?php
    session_start();
    if(!isset($_SESSION['rid']))$_SESSION['rid']=0;
    if(!empty($_POST['register']))
    {   
    $_SESSION['rid'] = $_SESSION['rid']+ 1;
require("fpdf/fpdf.php");
    $pdf=new FPDF();
    $pdf->Addpage();
$pdf->SetFont("Arial","B",18);
    $pdf->Cell(0,10,"Receipt number:".$_SESSION['rid'],0,0,'C');
    $pdf->output();
    }
?>

您在第一次迭代中有2个,因为您使用会话,在请求之间的会话中保存值
为什么使用会话?为此使用变量或在每次迭代上设置$_SESSION['rid'] = 0