php 单击 <a> 链接时如何设置会话变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4854754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:06:17  来源:igfitidea点击:

How to set a session variable when clicking a <a> link

phphtmlajaxsession

提问by Mixxiphoid

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

我有以下问题......我想在点击普通链接时设置一个会话变量,例如:

<a href="home" name="home">home</a>

My research seems to point out that it is not possible for PHP to catch up with the click event in a such a way that it would set a session variable.

我的研究似乎指出 PHP 不可能以设置会话变量的方式赶上单击事件。

I believe it is possible with Ajax, but how? And what would my link look like?
Setting the session variable should look like:

我相信 Ajax 是可能的,但是如何呢?我的链接会是什么样子?
设置会话变量应如下所示:

$_SESSION['link'] = home;

So in short: When clicking on a link in HTML, a session variable must be set.
HOW am i going to do that?

简而言之:单击 HTML 中的链接时,必须设置会话变量。
我该怎么做?

PS: I'm not quite familiar with Ajax, but I'll catch up.

PS:我对 Ajax 不是很熟悉,但我会赶上。

EDIT: The links will refer to the same page, also i want to avoid urls like "home.php?link=X".
If it isn't possible to do it any other way, too bad. But I'll hope there is a solution.
Important:the name of the link will be the value of $_SESSION['link']

编辑:链接将引用同一个页面,我也想避免像“home.php?link=X”这样的网址。
如果不能以任何其他方式做到这一点,那就太糟糕了。但我希望有一个解决方案。
重要提示:链接的名称将是$_SESSION['link']

采纳答案by SuperSpy

    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.

更新:尽管与@Brandon 几乎相同......只需使用一个 php 变量,我知道这看起来像一个安全风险,但如果正确完成它就不是。

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

PHP:

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.

为什么甚至将 GET 存储在会话中?就用它。请告诉我为什么您不想使用 GET。? 验证以提高安全性。我也许可以帮助您编写更好的脚本。

回答by as_bold_as_love

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).

我遇到了同样的问题 - 我想通过单击超链接将参数传递到另一个页面并获取转到下一页的值(不使用 GET,因为参数存储在 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.

对于那些不明白为什么要这样做的人,答案是您不希望用户看到敏感信息,或者您不希望有人编辑 GET。

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

在网上搜索之后,似乎不可能使用 POST 方法制作正常的超链接。

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

然后我有一个尤里卡的时刻!!!!为什么不直接使用 CSS 使提交按钮看起来像一个普通的超链接???...并将我想传递的值放入隐藏字段

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

我试过了,它有效。你可以在这里看到一个例子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:

基本的CSS是:

    #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;
    }

回答by Chugworth

In HTML:

在 HTML 中:

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

Then in PHP:

然后在 PHP 中:

if(isset($_GET['link'])){$_SESSION['link'] = $_GET['link'];}

回答by Brandon

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.

homge 页面会解析查询字符串并设置会话变量。