需要帮助修复我的错误注意:数组到字符串转换

时间:2013-11-17 07:22:41

标签: php arrays session

我遇到问题,了解我的错误代码。我要做的是在用户登录后,他们会被重定向到successful_login.php页面。从那里我想要检索他们的名字并通过会话在页面上回显它。错误代码为:注意:第23行的C:\ xampp \ htdocs \ Assignment_3 \ success_login.php中的数组到字符串转换 欢迎,数组

这是我的 login.php页面

<?php



//GOT USER ID COOKIE? IF SO GET IT
if(isset($_COOKIE["userid"])) {
    $username = $_COOKIE["userid"];
}
//NO USER ID? GET THE COOKIE
else {

    //SET COOKIE, EXPIRE IN 600 SECONDS
    setcookie("userid", $username, time()+600 );

}

//SESSION ID IS OUR UNIQUE COOKIE
session_id($username);

//STARTING SESSION
session_start();

$_SESSION['logged_in'] = true;

//TOTAL AMOUNT OF TIME USER WAS LOGGED IN
$_SESSION['time_logged_in'] = time();


//THIS PAGE WILL PROMPT THE USER TO LOGIN WITH AN ALREADY REGISTERED USERNAME AND PASSWORD IN THE DATABASE
//IT WILL CHECK IN THE DATABASE TO SEE IF THE USERNAME AND PASSWORD ARE IN THE SAME ARRAY
//IF NOT THEN IT WILL SAY LOGIN FAILE.

//INCLUDE FUNCTIONS
include "functions.inc";

//IF THE ERRORS ARE SET TO FALSE, ECHO THE ERROR MESSAGES
if (!isset($errors)) $errors = array();

//INITIATE VARIABLE TO ACCESS THE USER DATABASE
$userfile = "user_data.dat";
$currentuser = "curren_user.dat";

//IF THE LOGIN BUTTON WAS PRESSED THEN VALIDATE THAT THE USER AND PASSWORD ARE IN THE SAME ARRAY
//IF NOT, THEN ECHO ERROR MESSAGE.
//IF THEY MATCH, THEN GO TO THE SUCCESSFUL LOGIN PAGE.
if (array_key_exists("login_btn", $_POST))
{
    $logins = arrayfile_to_array($userfile);
    $userlogin = $_POST['user'];



    if (validate_login(strtolower($userlogin['name']), $userlogin['pass'], $logins))
    {
        $errors['login'] = "Invalid Login";
        //break; taken out 11/9/2013
    }
    if (count($errors) == null)
    {
        //QUESTIONABLE VARIABLE
        $_SESSION['username'] = $userlogin;
        header("Location: success_login.php");
    }
}


//IF THE USER HITS THE REGISTER NEW USER BUTTON,
//THEN TAKES THEM TO THE REGISTER NEW USER PAGE.
if (array_key_exists("register_btn", $_POST))
{
    header("Location: registration_page.php");
}

//PRINTING THE LOGIN USER INPUT FORMS.
//INCLUDES USERNAME AND PASSWORD.
//IF USERNAME AND PASSWORD DO NOT MATCH,
//HAS STUCKY FORM AND WILL ECHO ERROR MESSAGE AT TOP.
?>


<center>

<H1>Log In</H1>

<?php if (isset($errors['login'])) echo " <font color='red'>{$errors['login']}</font>"; ?>

<form method="POST" action="login.php"> 
    <table>
    <!--Text input for login-->
    <tr><td>Username:</td>
    <td><input type = 'TEXT'
           name = 'user[name]'
           value = '<?php  if(isset($_POST["user"])) {$tmp = $_POST["user"]; echo $tmp["name"]; } ?>'></td></tr>
    <br>
    <tr><td>Password:</td>
    <!--Password input for new password-->
    <td><input type = 'PASSWORD'
           name = 'user[pass]'
           value = '<?php  if(isset($_POST["user"])) {$tmp = $_POST["user"]; echo $tmp["pass"]; } ?>'></td></tr>
    <br>
    <tr></tr>
    <br>
    <!--Button to go back to form to fill again after errors are realized-->
    <tr><td><input type = 'SUBMIT'
         name = 'login_btn'
         value = 'Login'></td>
    <br>
    <!--Button to go back to form to fill again after errors are realized-->
    <td><input type = 'SUBMIT'
         name = 'register_btn'
         value = 'Register New User'></td></tr>    
    </table>
</form>

</center>
<?php 
//CLEARS THE ERRORS ARRAY
$errors = array(); 
?>

这是我的 success_login.php页面

<?php
session_start();



if (isset($_SESSION['username'])) {
   echo 'Welcome, '.$_SESSION['username']; 
} else {
echo 'Sorry, You are not logged in.';
}

?>
<form method='POST' action='invoice_display.php'>
<!--Displays Login successful page with button to redirect to invoice-->
    <center>

        <table>
            <tr><td><center><h1>Successfully Logged In!</center></h1></td></tr>
            <tr><td><center><h1><input type = submit value = "Go to Invoice" name ="sto_quantity"></center></h1></td></tr>
</form>

<form method='POST' action='index.php'>            
            <tr><td><center><h1><input type = submit value = "Go to Menu" name ="sto_products"></center></h1></td></tr>
</form>        
        </table>
    </center>

我对此非常陌生,所以任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

问题是..您在会话变量$_SESSION['username'];中存储了一个数组(这是可能的),但您在 $_SESSION['username']; <上显示为echo success.php /强>

解决方案

login.php 中,请执行此操作

if (count($errors) == null)
    {
        //QUESTIONABLE VARIABLE
        $_SESSION['username'] = $userlogin['name']; // <--------- Do this change
        header("Location: success_login.php");
    }