根据页面中设置的值在php中设置会话


Setting session in php from value being set in the page

我正在尝试将php会话设置为加载

 $_SESSION["tusername"] = $_POST['ttuser'];

但是,我的值是在$(function() { 上的jQuery中设置的

 $('#ttuser').val(tusername);

当页面被加载时,会话没有被设置,因为我相信会话是在jQuery设置文本框值之前设置的。我尝试使用ajax向页面发布一个值,但它无法检索

我现在设置会话的唯一方法是点击按钮,完全相同。

有什么想法吗?

提前感谢

编辑这是开始时的功能,根据的要求设置所有内容

$(function() {
  // Initialize. If we are already logged in, there is no
  // need for the connect button
  Twitch.init({clientId: CLIENT_ID}, function(error, status) {
    if (status.authenticated) {
      // we're logged in :)

      $('.authenticatedd').removeClass('hidden');
    } else {
      $('.welcometitle').html('<strong>Not yet connected with Twitch?</strong>');
      // Show the twitch connect button
      $('.authenticate').removeClass('hidden');
    }
  });

var token = Twitch.getToken();
      $('.twitch-connect').click(function() {
        Twitch.login({
          scope: ['user_read', 'channel_read', 'channel_editor', 'channel_commercial', 'user_subscriptions', 'channel_check_subscription']
        });
      })
Twitch.api({method: 'channel'}, function(error, channel) {
      $('#streamkey').text(channel.stream_key);
    });

    Twitch.api({method: 'user'}, function(error, user) {
    var tusername = user.display_name;
    var tlogo = user.logo;
    $('#twitchname').text(tusername);
    $('#ttuser').val(tusername);
$.get("setsession.php?ttuser="+tusername, function(){
});
    console.log(tlogo);
    if (tlogo != null)
    $('#twitchlogo').attr('src', tlogo);
$.cookie('logo', tlogo, { expires: 14, path: '/'});
    $('.sidename').html('<strong>' + tusername + '</strong> Logged in');
    var currentdate = new Date(); 
    var datetime = currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();    
    var items = [];
 items.push('<li><div class="xe-comment-entry"><div class="xe-comment"><a href="#" class="xe-user-name"><span class="label label-success">Status</span></a><p><span class="label label-danger">' +datetime+ '</span> Logged in</p></div></div></li>')
 $('#eventlog').prepend( items.join('') );
    });

});

为什么不直接向启动会话的php文件发出请求呢。

myfile.php

<?php
session_start();
$_SESSION["tusername"] = $_REQUEST['ttuser'];
echo "Session is : "+$_SESSION["tusername"];
?>

HTML

$(function(){
$.get("myfile.php?ttuser="+tusername, function(data){
console.log(data);
});
 $('#ttuser').val(tusername);
});