Need help fixing my error on Notice: Array to string conversion in

Romel

I'm having a problem understand my error code. What I'm trying to do is after the user logs in, they get redirected to the successful_login.php page. From there I want to retrieve their name and echo it on the page via sessions. The error code is: Notice: Array to string conversion in C:\xampp\htdocs\Assignment_3\success_login.php on line 23 Welcome, Array

here's my login.php page

<?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(); 
?>

here's my success_login.php page

<?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>

I'm really new at this so any help will be greatly appreciated.

Shankar Damodaran

The issue was .. you were storing an array inside the session variable $_SESSION['username']; (which is possible) , but you are displaying as echo $_SESSION['username']; on your success.php

Solution

In your login.php , do like this

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to solve PHP error 'Notice: Array to string conversion in...'

From Dev

PHP Notice: Array to string conversion Error

From Dev

need help fixing 500 error python/flask app

From Dev

Array to string conversion notice when using implode

From Dev

notice: array to string conversion in php

From Dev

PHP Notice: Array to string conversion on line 10

From Dev

Notice: Array to string conversion PHP

From Dev

Notice Array to string conversion using nusoap

From Dev

PHP: curl_setopt_array gives notice "array to string conversion"

From Dev

choice field symfony "Notice: Array to string conversion "

From Dev

Notice: Array to string conversion when using Foreach

From Dev

Need help fixing my implementation of RK4

From Dev

Notice Message: Array to string conversion

From Dev

Need help fixing a vba Error '1004' after editing a section of code

From Dev

Need help in identifying and fixing SSLPeerUnverifiedException

From Dev

PHP Notice: Array to string conversion on line 10

From Dev

Need help fixing my error on Notice: Array to string conversion in

From Dev

Notice: Array to string conversion with update statment

From Dev

choice field symfony "Notice: Array to string conversion "

From Dev

Multidimensional array from query into string gives Array to string conversion notice

From Dev

Need help fixing my isotope.js [jquery and html code]

From Dev

Notice: Array to string conversion $text = $htmlTag[0]

From Dev

FOSRest Produces a Notice: Array to string conversion

From Dev

Codeigniter: Severity notice Message Array to String conversion

From Dev

I need help fixing my code - very fragile

From Dev

Notice: Array to string conversion. without array in code

From Dev

JSON TO CSV Conversion Notice: Array to string conversion in

From Dev

Need help fixing my nginx server

From Dev

Notice: Array to String conversion - But it's a string in an array

Related Related

  1. 1

    How to solve PHP error 'Notice: Array to string conversion in...'

  2. 2

    PHP Notice: Array to string conversion Error

  3. 3

    need help fixing 500 error python/flask app

  4. 4

    Array to string conversion notice when using implode

  5. 5

    notice: array to string conversion in php

  6. 6

    PHP Notice: Array to string conversion on line 10

  7. 7

    Notice: Array to string conversion PHP

  8. 8

    Notice Array to string conversion using nusoap

  9. 9

    PHP: curl_setopt_array gives notice "array to string conversion"

  10. 10

    choice field symfony "Notice: Array to string conversion "

  11. 11

    Notice: Array to string conversion when using Foreach

  12. 12

    Need help fixing my implementation of RK4

  13. 13

    Notice Message: Array to string conversion

  14. 14

    Need help fixing a vba Error '1004' after editing a section of code

  15. 15

    Need help in identifying and fixing SSLPeerUnverifiedException

  16. 16

    PHP Notice: Array to string conversion on line 10

  17. 17

    Need help fixing my error on Notice: Array to string conversion in

  18. 18

    Notice: Array to string conversion with update statment

  19. 19

    choice field symfony "Notice: Array to string conversion "

  20. 20

    Multidimensional array from query into string gives Array to string conversion notice

  21. 21

    Need help fixing my isotope.js [jquery and html code]

  22. 22

    Notice: Array to string conversion $text = $htmlTag[0]

  23. 23

    FOSRest Produces a Notice: Array to string conversion

  24. 24

    Codeigniter: Severity notice Message Array to String conversion

  25. 25

    I need help fixing my code - very fragile

  26. 26

    Notice: Array to string conversion. without array in code

  27. 27

    JSON TO CSV Conversion Notice: Array to string conversion in

  28. 28

    Need help fixing my nginx server

  29. 29

    Notice: Array to String conversion - But it's a string in an array

HotTag

Archive