单击更改 php 会话值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19478793/
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
change php session value by click
提问by user2828251
i want to give a value to a session by clicking on
我想通过单击为会话赋值
i tried to do this , but its not working:
我试图这样做,但它不起作用:
<?php session_start();
$_SESSION['role']="";?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="auth-buttons.css" rel="stylesheet" />
<link href="StyleSheet.css" rel="stylesheet" />
</head>
<body>
<div id="wrap">
<div id="wrapHome">
<p><a class="btn-auth btn-facebook large" href="redirect.php" onclick="<?php $_SESSION['role']="facebook" ?>" > Sign in with <b>Facebook</b> </a></p>
<p><a class="btn-auth btn-twitter large" href="redirect.php" onclick="<?php $_SESSION['role']="twitter" ?>" > Sign in with <b>Twitter</b> </a></p>
<p><a class="btn-auth btn-google large" href="redirect.php" onclick="<?php $_SESSION['role']="google" ?>" > Sign in with <b>Google</b> </a></p>
</div>
</div>
</body>
</html>
采纳答案by DS.
'onclick' will not fire php code. It will trigger javascript though. You can use javascript to make AJAX calls to a php page that'd in turn be able to set your session values (and ajax would help you do so without a page refresh on the button click.
'onclick' 不会触发 php 代码。不过它会触发javascript。您可以使用 javascript 对 php 页面进行 AJAX 调用,该页面又可以设置您的会话值(而 ajax 可以帮助您完成此操作,而无需在单击按钮时刷新页面。
#('.btn-auth btn-facebook large').click(function(){
// fire off the request to /redirect.php
request = $.ajax({
url: "/redirect.php",
type: "post",
data: 'facebook'
});
// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
// log a message to the console
console.log("Hooray, it worked!");
});
// callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// log the error to the console
console.error(
"The following error occured: "+
textStatus, errorThrown
);
});
});
In your redirect.php
在你的redirect.php
<?php
$_SESSION['role'] = $_POST['data'];
?>
回答by Subin
A possible solution is to add a GETparameter in redirect.phpand change the SESSIONvariable in redirect.php.
一种可能的解决方案是添加一个GET参数redirect.php和改变SESSION变量redirect.php。
Change the following :
更改以下内容:
<p><a class="btn-auth btn-facebook large" href="redirect.php" onclick="<?php $_SESSION['role']="facebook" ?>" > Sign in with <b>Facebook</b> </a></p>
<p><a class="btn-auth btn-twitter large" href="redirect.php" onclick="<?php $_SESSION['role']="twitter" ?>" > Sign in with <b>Twitter</b> </a></p>
<p><a class="btn-auth btn-google large" href="redirect.php" onclick="<?php $_SESSION['role']="google" ?>" > Sign in with <b>Google</b> </a></p>
to :
到 :
<p><a class="btn-auth btn-facebook large" href="redirect.php?role=facebook"> Sign in with <b>Facebook</b> </a></p>
<p><a class="btn-auth btn-twitter large" href="redirect.php?role=twitter"> Sign in with <b>Twitter</b> </a></p>
<p><a class="btn-auth btn-google large" href="redirect.php?role=google"> Sign in with <b>Google</b> </a></p>
and add this at the top of the redirect.php
并将其添加到redirect.php的顶部
<?
session_start();
$_SESSION['role']=$_GET['role'];
?>
回答by Clarenceli
You can also add another php file to change session variable.
您还可以添加另一个 php 文件来更改会话变量。
Like this:
像这样:
<p><a class="btn-auth btn-facebook large" href="pass.php"> Sign in with <b>Facebook</b> </a></p>
and in pass.php
you have to add this code:
并且pass.php
您必须添加以下代码:
<?php
session_start();
$_SESSION['role']="facebook";
header("Location: your_first_php.php");
?>