use*_*251 4 php session redirect session-variables
我想通过点击为会话赋值
我试图这样做,但它不起作用:
<?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>
Run Code Online (Sandbox Code Playgroud)
一种可能的解决方案是添加一个GET参数redirect.php和改变SESSION变量redirect.php.
更改以下内容:
<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>
Run Code Online (Sandbox Code Playgroud)
至 :
<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>
Run Code Online (Sandbox Code Playgroud)
并将其添加到redirect.php的顶部
<?
session_start();
$_SESSION['role']=$_GET['role'];
?>
Run Code Online (Sandbox Code Playgroud)