4

我想通过单击为会话赋值

我试图这样做,但它不起作用:

<?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>
4

3 回答 3

8

一种可能的解决方案是在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'];
?>
于 2013-10-20T15:04:29.390 回答
3

'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'];

?>
于 2013-10-20T15:04:25.167 回答
1

您还可以添加另一个 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");
?>
于 2018-08-26T12:26:50.560 回答