如何在点击< a>时设置会话变量链接 [英] How to set a session variable when clicking a <a> link
问题描述
我有以下问题...我想在点击以下常规链接时设置会话变量:
< a href =homename =home>主页< / a>
我的研究似乎指出,PHP无法赶上点击事件一个这样的方式,它会设置一个会话变量。
我相信这可以通过Ajax实现,但是如何实现?我的链接是什么样子?
设置session变量应该如下所示:
$ _ SESSION [ 'link'] = home;
简而言之:点击HTML中的链接时,必须设置会话变量。 >
我该怎么做?
PS:我对Ajax不太熟悉,但我会赶上。
编辑:链接将引用相同的页面,我也想避免像home.php?link = X的网址。
如果它不是'不可能以任何其他方式去做,太糟糕了。但我希望有一个解决方案。
重要提示:链接的名称将是 $ _ SESSION ['link']
session_start();
if(isset($ _ SESSION ['current'])){
$ _SESSION ['oldlink'] = $ _ SESSION ['current'];
} else {
$ _SESSION ['oldlink'] ='没有上一页';
}
$ _SESSION ['current'] = $ _ SERVER ['PHP_SELF'];
也许这就是你要找的东西?
它会记住您来自的旧链接/页面(在您的网站内)。
将每一页放在每页顶部。
>如果您想使其'刷新证明',您可以添加另一个检查:
if(isset($ _ SESSION ['current'])&& $ _SESSION ['current']!= $ _ SERVER ['PHP_SELF'])
这会让网页不记得它自己。
更新:几乎相同as @Brandon虽然...
只需使用一个php变量,我知道这看起来像是一个安全风险,但是当它完成后,它不是。
< a href =home.php?a = register>立即注册!< / a>
PHP:
if(isset($ _ GET ['a'])/ *你可以在这里验证链接* /){
$ _SESSION ['link'] = $ _ GET ['a'];
}
为什么甚至将GET存储在会话中?只要使用它。
请告诉我为什么你不想使用GET。 «验证更安全。
我也许可以帮助您制作更好的脚本。
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.
I believe it is possible with Ajax, but how? And what would my link look like?
Setting the session variable should look like:
$_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?
PS: I'm not quite familiar with Ajax, but I'll catch up.
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']
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.
这篇关于如何在点击< a>时设置会话变量链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!