donglu9743 2015-12-04 07: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 08: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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部