0

im fairly new to php im trying to create a login, ive been trying to figure out what is wrong for past 2 days and have tried everything but could not find a solution to this, ive asked people looked all over the net but could not find a proper solution.

I keep recieving this error when i try and login '

Notice: Array to string conversion in __________ on line 6 Welcome Array.

it seems like the array is causing the issue when i login i want it to display the users first name and last name. here is my code thanks in advance everyone :)

Login.php
<?php
require_once('connect.php');
include('includes/head.php');
 ?>

 <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" >
 <div class="container">
 <fieldset>
 <h2>Login</h2>
 <div class="row">
 <label class="fixedwidth">Username:</label>
 <input type="text" name="username" required />
 </div>
 <div class="row">
 <label class="fixedwidth">Password:</label>
 <input type="password" name="password" />
 </div>
 <div class="row">
 <input type="submit" name="submit" value="LogIn" />
 </div>
 </fieldset>
 </div>
 </form>

 <?php

 if(isset($_POST['submit']))  {
 require_once('connect.php');
 $username = mysqli_real_escape_string($connection, $_POST['username']);
 $password = mysqli_real_escape_string($connection, $_POST['password']);

 if(!empty($username) && !empty($password))  {




    $query = "SELECT * FROM user where username='$username' and 
    password='$password'";
    $result = mysqli_query($connection, $query);
    $row=mysqli_fetch_array($result);
    mysqli_close($connection);
    if(mysqli_num_rows($result) ==1)  {


   $fullname=array(firstname=>$row['firstname'],lastname=>$row['lastname']);
    session_start();

    $_SESSION['user'] = $fullname;

    header('Location: welcome.php');

    }
    else    {
        echo "<p>Could not find you in the database.</p>";
    }
    }
    else    {
    echo "<p>Either the username or password are invalid. Please try 
    again</p>";
    }


     }
     ?>     





Logout.php
<?php
session_start();

unset($_SESSION['user']);
// remove all session variables


session_destroy();
// destroy the session  

header('Location: welcome.php');

?>




welcome.php
<?php
include('includes/head.php');
include('includes/nav2.php');
session_start();

echo 'Welcome '. $_SESSION['user'];
echo '<br><br>';
?>
4
  • Harish's implode method seemed to work the best but now im recieving this error Notice: Undefined index: user in welcome.php on line 6 Warning: implode(): Invalid arguments passed in welcome.php on line 6 Welcome , does anyone know what could solve this? Commented May 25, 2018 at 5:00
  • That would mean that session is empty. Commented May 25, 2018 at 5:46
  • You're wide open to SQL Injection attacks, you should use prepared statements. Also passwords should NEVER be stored in plain text form, they should ALWAYS be hashed Commented May 25, 2018 at 5:56
  • Don't forget to tick the right solution@SilentChef187 Commented May 25, 2018 at 6:11

5 Answers 5

1

You cannot echo an array.
So you have to implode the array like this:

echo 'Welcome '.implode(" ", $_SESSION['user']);

But this will give you the Warning: implode(): Invalid arguments passed in welcome.php because you unset the $_SESSION['user'] when you logout.
So you have to check it with isset like this:

echo isset($_SESSION['user']) ? 'Welcome '.implode(" ", $_SESSION['user']) : "Whatever you want when user is not logged in";
Sign up to request clarification or add additional context in comments.

Comments

0

What you do is that you put an array into $_SESSION['user']. If you try to convert array to string it won't work as for example in JavaScript.

You need to combine first name and lastname from the array:

echo 'Welcome ' . $_SESSION['user']['firstname'] . ' ' . $_SESSION['user']['lastname']

Comments

0

In your welcome.php file, you have written

echo 'Welcome '. $_SESSION['user'];

But the variable $_SESSION['user'] is an array which you are trying to concatenate with a string. That is the main issue. Arrays cannot be concatenated with a string.

So to solve this issue, you can do this

echo 'Welcome '. implode(" ", $_SESSION["user"]);

Hope this helps.

2 Comments

You are a legend sir! your method worked the best thank you so much! but now when i logout i have this error : Notice: Undefined index: user in welcome.php on line 6 Warning: implode(): Invalid arguments passed in welcome.php on line 6 Welcome , do you know what could solve this?
Just check if the session variable exists or not using isset();
0

1-first you need to do remove mysqli_close($connection); from there.you can put it at the end of the script.because you are trying to close connection here and after that you are trying to count row too.which could create problem.

2-you have array.and you are trying to use with out extracting.see

$fullname=array(firstname=>$row['firstname'],lastname=>$row['lastname']);

3-i recommend you remove this from your code

$fullname=array('firstname'=>$row['firstname'],'lastname'=>$row['lastname']);

and try like this.

$_SESSION['user'] = $row['firstname'];

Comments

0

echo isset($_SESSION['user']) ? 'Hello'.implode(" ", $_SESSION['user']) : "Whatever you want when user is not logged in";

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.