我想通过点击
为会话赋值我试图这样做,但它不起作用:
<?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>
答案 0 :(得分:5)
一种可能的解决方案是在 redirect.php 中添加 GET 参数,并在 redirect.php中更改 SESSION 变量< /强>
更改以下内容:
<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>
到:
<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>
并将其添加到 redirect.php
的顶部<?
session_start();
$_SESSION['role']=$_GET['role'];
?>
答案 1 :(得分:2)
'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
);
});
});
在你的redirect.php
中<?php
$_SESSION['role'] = $_POST['data'];
?>
答案 2 :(得分:0)
您还可以添加另一个php文件来更改会话变量。
赞:
<p><a class="btn-auth btn-facebook large" href="pass.php"> Sign in with <b>Facebook</b> </a></p>
,并且必须在pass.php
中添加以下代码:
<?php
session_start();
$_SESSION['role']="facebook";
header("Location: your_first_php.php");
?>