2012-09-04 141 views
-5

我正在制作login.php,如果成功,它将返回home.php。我想用$ _SESSION,这里是我的代码:(!)注意:未定义的索引:用户名在C: wamp www HavenInWonderland home.php在线15

的login.php

<?php require_once('Connections/conn.php'); ?> 
<?php 
if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{ 
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

    switch ($theType) { 
    case "text": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break;  
    case "long": 
    case "int": 
     $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
     break; 
    case "double": 
     $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; 
     break; 
    case "date": 
     $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
     break; 
    case "defined": 
     $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
     break; 
    } 
    return $theValue; 
} 
} 
?> 
<?php 
// *** Validate request to login to this site. 
if (!isset($_SESSION)) { 
    session_start(); 
} 

$loginFormAction = $_SERVER['PHP_SELF']; 
if (isset($_GET['accesscheck'])) { 
    $_SESSION['PrevUrl'] = $_GET['accesscheck']; 
} 

if (isset($_POST['username'])) { 
    $loginUsername=$_POST['username']; 
    $password=$_POST['pass']; 
    $MM_fldUserAuthorization = "username"; 
    $MM_redirectLoginSuccess = "home.php"; 
    $MM_redirectLoginFailed = "login.php"; 
    $MM_redirecttoReferrer = false; 
    mysql_select_db($database_conn, $conn); 

    $LoginRS__query=sprintf("SELECT username, pass, username FROM member WHERE username=%s AND pass=%s", 
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 

    $LoginRS = mysql_query($LoginRS__query, $conn) or die(mysql_error()); 
    $loginFoundUser = mysql_num_rows($LoginRS); 
    if ($loginFoundUser) { 

    $loginStrGroup = mysql_result($LoginRS,0,'username'); 

    //declare two session variables and assign them 
    $_SESSION['MM_Username'] = $loginUsername; 
    $_SESSION['MM_UserGroup'] = $loginStrGroup;  

    if (isset($_SESSION['PrevUrl']) && false) { 
     $MM_redirectLoginSuccess = $_SESSION['PrevUrl']; 
    } 
    header("Location: " . $MM_redirectLoginSuccess); 
    } 
    else { 
    header("Location: ". $MM_redirectLoginFailed); 
    } 
} 
?> 
<html> 
<head> 
    <title>session</title> 
</head> 

<body> 
<form method="POST" action="<?php echo $loginFormAction; ?>"> 
    Please enter you name: 
    <input type="text" id="username" name="username" /> 
    <input type="password" id="pass" name="pass" /> 

<input type="submit" value="Login" /> 
    </form> 
</body> 
</html> 

,这里是我的HOME.php

<?php 
session_start(); 

?> 

<html> 
    <head> 
    <title>welcome</title> 
    </head> 

    <body> 
    <h1>Thnx for signing in</h1> 
    <?php 
    echo "Welcome" . " " . $_POST['username']; 
    $_SESSION['chorva'] = $_POST['username']; 




    ?> 

    </body> 
</html> 

我有用户名作为我的文本框的名字这就是我放在home.php上的原因,但为什么它总是说我有一个未定义的索引?

请别人帮我吗?

+0

当你重定向'$ _POST'变量都将丢失,所以'username'始终将是一个不确定的指数。 – froddd

+0

因为'$ _POST'数组是空的,或者至少没有''用户名'键,因为你没有在当前请求中发送任何东西。 – deceze

+0

因为'$ _POST'是空的。当您使用行动'a.php'和'a.php'提交表单时,您将重定向到'b.php'发布的数据将不会被发送。把'$ _SESSION ['username'] = $ _POST ['username']'放在login.php中,然后在'session_start'之后使用它。 – Leri

回答

0

那是因为$_POST['username']不存在,这将是永远的空当您重定向他到另一个页面,而不是我看到你分配给他的会话的用户名和你可以做:

也许你想要的东西像这样:

<?php 
    if(isset($_SESSION['MM_Username'])){ 
     echo "Welcome ".$_SESSION['MM_Username']; 
    } 
?> 
+0

哇!非常感谢!感谢您的帮助, – aianananalalala

1

你需要使用

<?php 
session_start(); 

?> 

<html> 
    <head> 
    <title>welcome</title> 
    </head> 

    <body> 
    <h1>Thnx for signing in</h1> 
    <?php 
    echo "Welcome" . " " . $_SESSION['MM_Username']; 
    $_SESSION['chorva'] = $_SESSION['MM_Username']; 




    ?> 

    </body> 
</html> 

既然您已经定义了$ _SESSION [ 'MM_Username']中的login.php会话的用户名

+0

非常感谢你! :) – aianananalalala

1

改变这一行:

echo "Welcome" . " " . $_POST['username'];

echo "Welcome" . " " . isset($_POST['username']) ? $_POST['username'] : '';

相关问题