2014-04-01 93 views
-4

这里的代码工作得很好。我想要做的就是传递已存储在$ userName中的FirstName和LastName(使用SESSION)。尝试登录时,实际上从数据库中检索到名字和姓氏。 我希望有名字和姓氏显示在每个网页上登录后..谢谢会话变量动态传递到下一页

<table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> 
<tr> 
<td><?php 
$DisplayForm = TRUE; 
$errors = 0; 

if(isset($_POST['uname']) && isset($_POST['pass'])) { 
include("dbconnect.php"); 
if($DBConnect !== FALSE){ 
$SQLstring = "SELECT userid, first_name, last_name FROM users WHERE username ='". 
$_POST['uname']. "' and password = '".md5($_POST['pass'])."'"; 
$DisplayForm = FALSE; 
$QueryResult = @mysql_query($SQLstring, $DBConnect); 
echo mysql_error(); 
if (mysql_num_rows($QueryResult)=== 0){ 
    echo "<p style='color:red;'><b>&nbsp;&nbsp;&nbsp;The email address/password " . 
    " combination is not valid.</b></p>\n"; 
    ++$errors; 
    $DisplayForm = TRUE; 
} 
else 
{ 
    $DisplayForm = FALSE; 
} 

} 

} 

if ($DisplayForm) 
{?> 


<form id="form1" name="loginForm" method="post" action="index.php"> 
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> 
<tr> 
    <td width="93" bgcolor="#DACFAF"><strong> &nbsp;&nbsp;&nbsp;Username:</strong></td> 
    <td width="149" bgcolor="#DACFAF"><label for="textfield"></label> 
    <input type="text" name="uname" id="textfield" /></td> 
    <td width="76" bgcolor="#DACFAF"><strong>Password:</strong></td> 
    <td width="150" bgcolor="#DACFAF"><label for="textfield2"></label> 
    <input type="password" name="pass" id="pass" /></td> 
    <td width="196" bgcolor="#DACFAF"><input type="image" name="login" src="images/login.jpg" />&nbsp;</td> 
    <td width="68" bgcolor="#DACFAF">&nbsp;</td> 
    <td width="68" bgcolor="#DACFAF"><strong><a href="register.php">Register</a> </strong> </td> 
    </tr> 
</table> 
    </form> 

<?php } 
    else { 
    $Row = mysql_fetch_assoc($QueryResult); 
    $userID = $Row['userid']; 
    $userName = $Row['first_name']. " ". $Row['last_name']; 


    echo "<table width=780px border=0px >"; 
       echo "<tr>"; 
       echo "<td style='color:red;'>"; 
       echo "<b>&nbsp;&nbsp;&nbsp;Welcome back, $userName!</b>"; 
       echo "</td>"; 
       echo"<td align='right'>"; 
       echo "<form method='POST' action=''>"; 
       echo "<input type='submit' name='submit' value='logout'>"; 
       echo "</form>"; 
       echo "</td>"; 
       echo "</tr>"; 
       echo "</table>"; 


    } 


?>&nbsp;</td> 
    </tr> 
</table> 
+0

_什么是你的问题?_ –

+0

你现在得到什么结果? –

+0

我不确定如何真正通过会话。我可以在表单之间传递,但动态并不真正起作用。 – user3479738

回答

0

假设你知道如何设置$ _SESSION变量。

,或者如果没有

在你的日志在PHP中使用session_start();启动一个会话。毕竟检查完成后。将名称放在$_SESSION变量中。

这样

$_SESSION['username'] = $firstname.$lastname;

假设$名字有用户的名字和姓氏$具有用户的姓氏。

THEN:

session_start();在你的PHP文件的最开始,你要显示的用户名。

然后

$userName=$_SESSION['username'];

在每个PHP文件的最开始将这个两者结合起来,你要显示的名称。 现在您可以使用变量$userName来显示名称。

+0

谢谢你..会努力工作并回复你..谢谢你。 – user3479738

+0

谢谢。我可以通过添加$ _SESSION ['username'] =“$ userName”来修复它;我之前遇到过的问题是,我将它添加到 – user3479738

+0

页顶部声明的会话中。很高兴我能帮上忙 :) –