Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 设置时出现问题$_会话[&"x27;用户名&"x27;];_Php_Session - Fatal编程技术网

Php 设置时出现问题$_会话[&"x27;用户名&"x27;];

Php 设置时出现问题$_会话[&"x27;用户名&"x27;];,php,session,Php,Session,我的脚本在我刚刚将用户名手册设置为Admin时工作,但现在我需要将用户名设置为“$\u SESSION['username']”,这会给我的代码带来一些麻烦 我试过用不同的方式设置它,现在我不知道是否有更多的问题,或者只是用户名不起作用 我的代码是: <?php include "connect.php"; include "head.php"; ?> <?php $action = $_GET['action']; if($action == "fight"

我的脚本在我刚刚将用户名手册设置为Admin时工作,但现在我需要将用户名设置为“$\u SESSION['username']”,这会给我的代码带来一些麻烦

我试过用不同的方式设置它,现在我不知道是否有更多的问题,或者只是用户名不起作用

我的代码是:

    <?php
 include "connect.php";
 include "head.php";
 ?>

<?php

$action = $_GET['action'];
if($action == "fight"){
$checkfight = @mysql_fetch_array(@mysql_query("SELECT * FROM fights WHERE User=".$_SESSION['Username']."")); //Check if fight exist
$monstername = $checkfight['monstername'];
$monsterlevel = $checkfight['monsterlevel'];
$monsterdice = $checkfight['monsterdice'];
$monsterhp = $checkfight['monsterhp'];
$monstermana = $checkfight['monstermana'];
$monsterarmor = $checkfight['monsterarmor'];
$monstermindmg = $checkfight['monstermindmg'];
$monstermaxdmg = $checkfight['monstermaxdmg'];

if($checkfight['User'] == 'Admin') {

$selectuser = @mysql_fetch_array(@mysql_query("SELECT * FROM members WHERE Username=".$_SESSION['Username']."")); //loading the user
$currently = $selectuser['Quest'];
$userhp = $selectuser['Life'];
$usermana = $selectuser['Mana'];
$usermindmg = $selectuser['mindmg'];
$usermaxdmg = $selectuser['maxdmg'];
$userdice = $selectuser['dice'];

$fightMonster = @mysql_fetch_array(@mysql_query("UPDATE fights SET Player1hp='$userhp', Player1mana='$usermana', Player1min='$usermindmg', Player1max='$usermaxdmg' WHERE User=".$_SESSION['Username']."")); 

} else {

$selectuser = @mysql_fetch_array(@mysql_query("SELECT * FROM members WHERE Username=".$_SESSION['Username'].""));
$currently = $selectuser['Quest'];
$userhp = $selectuser['Life'];
$usermana = $selectuser['Mana'];
$usermindmg = $selectuser['mindmg'];
$usermaxdmg = $selectuser['maxdmg'];
$userdice = $selectuser['dice'];

$MonsterID = $_GET['monster'];
$selectMonster = @mysql_fetch_array(@mysql_query("SELECT * FROM monsters WHERE monsterID='$MonsterID'")); 
$monstername = $selectMonster['monstername'];
$monsterlevel = $selectMonster['monsterlevel'];
$monsterdice = $selectMonster['monsterdice'];
$monsterhp = $selectMonster['monsterhp'];
$monstermana = $selectMonster['monstermana'];
$monsterarmor = $selectMonster['monsterarmor'];
$monstermindmg = $selectMonster['monstermindmg'];
$monstermaxdmg = $selectMonster['monstermaxdmg'];

$savefight = @mysql_fetch_array(@mysql_query("INSERT INTO fights SET User=".$_SESSION['Username'].", Player1hp='$userhp', Player1mana='$usermana', Player1min='$usermindmg', Player1max='$usermaxdmg', monstername='$monstername', monsterlevel='$monsterlevel', monsterdice='$monsterdice', monsterhp='$monsterhp', monstermana='$monstermana', monsterarmor='$monsterarmor', monstermindmg='$monstermindmg', monstermaxdmg='$monstermaxdmg'"));

}

?> 


<div id="fightcontent">

<div id="fight1">

Health: <?php echo $userhp; ?> <br>
Mana: <?php echo $selectuser['Mana'];?><br>
Damage: <?php echo $selectuser['mindmg']; echo "-"; echo $selectuser['maxdmg'];?>
<br>
<br>
Dice: <?php echo $userdice; ?>
<br>
Quick Slots (3)

  </div>
  <div id="fight2">

Health: <?php echo $monsterhp; ?>
<br>
<?php
if($monstermana == "1") {
echo "Mana: $monstermana";
} else {}
?>
<?php
if($monsterarmor == "0") {
} else { echo "Armor: $monsterarmor"; }
?>
Damage: <?php echo $monstermindmg; echo "-"; echo $monstermaxdmg;?>
<br>
<br>
Dice: <?php echo $monsterdice; ?>
<br>
Spells / Quickslots

  </div>
  <div id="dice">

<?php
if($_POST['submit']) {
$selectuser = @mysql_fetch_array(@mysql_query("SELECT * FROM members WHERE Username=".$_SESSION['Username'].""));
$userdice = $selectuser['dice'];
$usermindmg = $selectuser['mindmg'];
$usermaxdmg = $selectuser['maxdmg'];


$MonsterID = $_GET['monster'];
$selectMonster = @mysql_fetch_array(@mysql_query("SELECT * FROM monsters WHERE monsterID='$MonsterID'")); 
$monsterdice = $selectMonster['monsterdice'];


?>

<?php
if($userdice == 1){ 
$player1roll1 = rand(1,6); 
$player1roll2 = 0; 
$player1roll3 = 0; 
}elseif($userdice == 2){
$player1roll1 = rand(1,6);
$player1roll2 = rand(1,6);
$player1roll3 = 0; 
}elseif($userdice == 3){
$player1roll1 = rand(1,6); 
$player1roll2 = rand(1,6); 
$player1roll3 = rand(1,6); 
}
?>


<?php

if($monsterdice == 1){ 
$player2roll1 = rand(1,6); 
$player2roll2 = 0; 
$player2roll3 = 0; 
} elseif($monsterdice == 2){
$player2roll1 = rand(1,6);
$player2roll2 = rand(1,6);
$player2roll3 = 0; 
}elseif($monsterdice == 3){
$player2roll1 = rand(1,6); 
$player2roll2 = rand(1,6); 
$player2roll3 = rand(1,6); 
}
?>

<?php


$player1sum = $player1roll1 + $player1roll2 + $player1roll3;
$player2sum = $player2roll1 + $player2roll2 + $player2roll3;

if($player1sum > $player2sum) {
$playerdmg = rand($usermindmg,$usermaxdmg);
$monsterhpnew = $monsterhp - $playerdmg;
$fightMonster2 = @mysql_fetch_array(@mysql_query("UPDATE fights SET Player1hp='$userhp', Player1mana='$usermana', Player1min='$usermindmg', Player1max='$usermaxdmg', monsterdice='$monsterdice', monsterhp='$monsterhpnew', monstermana='$monstermana', monsterarmor='$monsterarmor' WHERE User=".$_SESSION['Username']."")); 

echo "You hit ";
echo $monstername ;
echo ", with" ;
echo $playerdmg ;
echo "damage. remaining health:";
echo $monsterhpnew;
echo "<br>";

if($monsterhpnew < 1){
$updatefight = @mysql_fetch_array(@mysql_query("UPDATE members SET Fight=0 WHERE Username='$user'")); 
$deletefight = @mysql_fetch_array(@mysql_query("DELETE FROM fights WHERE User='$user'")); 
header("Location: quest.php?action=victory&ID=$currently");
echo "You killed him..."; 
}


}elseif($player2sum > $player1sum) {
echo "Player 2 won";
echo $player1sum ;
echo $player2sum ;
}else{
echo "Draw";
}
echo '<img src="images/dice/'.$player1roll1.'.gif" />'; //echo the image for roll 1 of player 1
echo '<img src="images/dice/'.$player1roll2.'.gif" />'; //echo the image for roll 2 of player 1
echo '<img src="images/dice/'.$player1roll3.'.gif" />'; //echo the image for roll 2 of player 1
echo " VS. ";
echo '<img src="images/dice/'.$player2roll1.'.gif" />'; //echo the image for roll 1 of player 2
echo '<img src="images/dice/'.$player2roll2.'.gif" />'; //echo the image for roll 2 of player 2

}else{
?>


<?php } ?> 

<form method="post">
<input type="submit" name="submit" value="Roll dice" />
</form>

<?php } ?>


</div></div>


在使用$会话变量之前,必须调用
SESSION\u start()

在任何会话代码之前调用此函数。尽管我可能会建议大家多读一点OO PHP和一些框架


这可能是一个很好的开始,有助于你未来的编程生涯。

我看不到
会话\u start()

如果您想访问$\u会话

您可能还想在SQL查询中尝试传递或收集的值周围加引号:
Username='“$\u会话”。”“
也包括上述所有注释:)

您需要使用会话变量。只需将
session\u start()
放在脚本的第一行

避免使用mysql,这是很重要的

您不需要连接
“string”$var。“string”
,您可以使用
“string{$var}string”
,甚至更好。使用PDO,它就像

$query = $db->prepare("SELECT column FROM table WHERE row1 = ?, row2 = ?");
$query->execute(array($var1, $var2));

这可以避免并使代码更干净。

1。停止使用
@
抑制错误。2.停止使用
mysql\uuz
函数,它们已被弃用。3.使用绑定的参数和准备好的语句。在使用$\u会话数组之前,是否确实调用了
session\u start()
。在已经是字符串的内容周围添加引号是毫无意义的,而且您似乎不了解$\u会话变量是如何工作的。感谢您让我的脚本再次工作:)是的,我忘记启动会话了!带着新问题回来,看到它已经准备好回答,这是多么酷啊D(用户名=“$”会话。“”)非常感谢..而且你可能还没有见过我的最后一个;)