How to set a session variable when clicking a link

后端 未结 4 1713
不知归路
不知归路 2020-12-24 08:40

I have the following problem... I want to set a session variable when clicking on a normal link like:

home
         


        
4条回答
  •  别那么骄傲
    2020-12-24 08:54

        session_start();
        if(isset($_SESSION['current'])){
             $_SESSION['oldlink']=$_SESSION['current'];
        }else{
             $_SESSION['oldlink']='no previous page';
        }
        $_SESSION['current']=$_SERVER['PHP_SELF'];
    

    Maybe this is what you're looking for? It will remember the old link/page you're coming from (within your website).

    Put that piece on top of each page.

    If you want to make it 'refresh proof' you can add another check:

       if(isset($_SESSION['current']) && $_SESSION['current']!=$_SERVER['PHP_SELF'])
    

    This will make the page not remember itself.

    UPDATE: Almost the same as @Brandon though... Just use a php variable, I know this looks like a security risk, but when done correct it isn't.

     Register Now!
    

    PHP:

     if(isset($_GET['a']) /*you can validate the link here*/){
        $_SESSION['link']=$_GET['a'];
     }
    

    Why even store the GET in a session? Just use it. Please tell me why you do not want to use GET. « Validate for more security. I maybe can help you with a better script.

提交回复
热议问题