donglu9743 2015-12-04 15:44
浏览 16
已采纳

如何在php $ _SESSION中存储多个单词?

Bit of a newbie here and hoped someone could help me?

All i'm trying to do is store more then one word in a $_SESSION. I've done a MySQL query to retrieve a member name from a database and this returns "Mr Brown". I've then stored the $memberName in a session like this:

$_SESSION['MemberName'] = $memberName;

If I echo straight after, it does give me "Mr Brown". But if I redirect to another page (header( 'Location: ../memberIndex.php' );) it doesn't get stored! It works ok if the $memberName is one word like "Bob", but as soon as there is a space in it, it doesn't transfer over. Other variables are stored ok as there ID numbers etc, its just this one? Can anyone help please? I've googled for ages and I can't see anything that lets me put two or more words into a $_SESSION variable?!? ...HELP!

<?
session_start(); 

$HTTP_POST_VARS["submit"]; 
$Username = $_POST['username']; 
$Password = $_POST['password']; 

$query = "SELECT * FROM MemberLogin WHERE username = '$Username' AND password = '$Password'"; 

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) 
{ 
   extract($row); 
   $_SESSION['MemberID'] = $id;
   $_SESSION['MemberName'] = $memberName;

   //echo $_SESSION['MemberName'];

   header( 'Location: ../memberIndex.php' ) ; 
}

Ok, bit of an update. I've just tried another script:

<?
session_start();

$_SESSION['MemberName'] = "Matt Brown";

//echo $_SESSION['MemberName'];

header( 'Location: testSession2.php' ) ;

?>

And when its transferred to testSession2.php, it works! So whats the difference? Where have I gone wrong? If I allocate the _SESSION with "Matt Brown", its ok. But if I allocated the _SESSION with $memberName that has "Matt Brown" in it, it doesn't?!? Oh i'm so confused!

  • 写回答

1条回答 默认 最新

  • dongyuan3094 2015-12-04 16:35
    关注

    Try wrapping $memberName in double quotes. Not sure if that will work though.

    Also, please use PDO instead of the mysql extension-it's much safer: http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛