【问题标题】:change php session value by click通过单击更改 php 会话值
【发布时间】:2013-10-29 00:24:12
【问题描述】:

我想通过点击为会话赋值

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

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

【问题讨论】:

  • 您需要 AJAX 才能做到这一点。阅读它。

标签: php session redirect session-variables


【解决方案1】:

一种可能的解决方案是在 redirect.php 中添加 GET 参数,并在 redirect.phpSESSION 变量/strong>。

更改以下内容:

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

【讨论】:

  • 我相信这不会实现他正在寻找的东西。这将在按钮单击时将他带到另一个页面。我认为他需要使用 AJAX。
  • 不,他已经在 url 中设置了 href 属性。我认为他想在去redirect.php 之前更改会话值。答案执行相同的任务,但方式不同。
  • 非常感谢!这两个答案都帮助了我......但现在还有一个问题 - “重定向页面”移动到第三页 - 我也需要那里的会话......
  • @user2828251,一旦您设置了会话变量,并且您的会话在所有页面上都处于活动状态,您在 redirect.php 中所做的更改也应该在您的第三个页面上可用。
  • @Subin onclick="this.disabled=true" 如果页面刷新禁用事件未设置,如果页面刷新如何使保持禁用?
【解决方案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'];

?>

【讨论】:

    【解决方案3】:

    您还可以添加另一个 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");
    ?>
    

    【讨论】:

      猜你喜欢
      • 2013-07-12
      • 1970-01-01
      • 2023-04-04
      • 2022-12-15
      • 2010-12-04
      • 2014-02-03
      • 2021-10-20
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多