How to set a session variable when clicking a link

后端 未结 4 1685
不知归路
不知归路 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.

     <a href="home.php?a=register">Register Now!</a>
    

    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.

    0 讨论(0)
  • 2020-12-24 08:54

    In HTML:

    <a href="index.php?link=home" name="home">home</a>
    

    Then in PHP:

    if(isset($_GET['link'])){$_SESSION['link'] = $_GET['link'];}
    
    0 讨论(0)
  • 2020-12-24 08:59

    Is your link to another web page? If so, perhaps you could put the variable in the query string and set the session variable when the page being linked to is loaded.

    So the link looks like this:

    <a href="home.php?variable=value" name="home">home</a>
    

    And the homge page would parse the query string and set the session variable.

    0 讨论(0)
  • 2020-12-24 09:05

    I had the same problem - i wanted to pass a parameter to another page by clicking a hyperlink and get the value to go to the next page (without using GET because the parameter is stored in the URL).

    to those who don't understand why you would want to do this the answer is you dont want the user to see sensitive information or you dont want someone editing the GET.

    well after scouring the internet it seemed it wasnt possible to make a normal hyperlink using the POST method.

    And then i had a eureka moment!!!! why not just use CSS to make the submit button look like a normal hyperlink??? ...and put the value i want to pass in a hidden field

    i tried it and it works. you can see an exaple here http://paulyouthed.com/test/css-button-that-looks-like-hyperlink.php

    the basic code for the form is:

        <form enctype="multipart/form-data" action="page-to-pass-to.php" method="post">
                            <input type="hidden" name="post-variable-name" value="value-you-want-pass"/>
                            <input type="submit" name="whatever" value="text-to-display" id="hyperlink-style-button"/>
                    </form>
    

    the basic css is:

        #hyperlink-style-button{
          background:none;
          border:0;
          color:#666;
          text-decoration:underline;
        }
    
        #hyperlink-style-button:hover{
          background:none;
          border:0;
          color:#666;
          text-decoration:none;
          cursor:pointer;
          cursor:hand;
        }
    
    0 讨论(0)
提交回复
热议问题