2

Okey so i got a problem when a user changes their profile picture. And if I with my silly mind understood correctly (what i think) this is due to my session being not updated... So this is my uploadfile where the user is able to upload a image file to the database, the image gets inserted and it works fine but even if the image is changed in the database i still see the old image when i insert it... Only when i logout and login im able to see the new picture,

  • Is there some type of way i can update my session without the user being logged out in the proccess....?
  • Is this due to my session being unupdated?

Don't know if it helps but here is my code

<?php
if(isset($_FILES['file']) )
{

move_uploaded_file($_FILES['file']['tmp_name'],'files/'.$_FILES['file']['name']);

session_start();
$username = $_SESSION['user'];
$userpic = 'files/'.$_FILES['file']['name'];
$id = $_SESSION['id'];

include ("connect.php");
$sql = $con->prepare('UPDATE users SET username=?, userpic=? WHERE id = ?');
$sql->bind_param("ssi",$username,$userpic,$id);
$sql->execute(); 
$sql->close();
$con->close();  
echo '<div id = "check"> Your image was succesfully uploaded</div>';
}
else{
echo "no files";}
?>

And here is my login if that makes it easier as well:

<?php 

include('connect.php');

$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users";

$result = $con->query($query);

while($row = $result->fetch_object())
{
    if($username == $row->username)
    {

            $checkPassword = password_verify($password,$row->password);
            if($checkPassword ){   // betyder om det är sant
                session_start();
                $_SESSION['loggedIn'] = true; 
                $_SESSION['user'] = $row->username;
                $_SESSION['admin'] = $row->admin;
                $_SESSION['userpic'] = $row->userpic;
                $_SESSION['id'] = $row->id;
                header("Location:music.php?success");
                exit();
                $fail = false;
            }


    }
    else{
        $fail = true;
        if($fail){      
    echo "<script>
    alert('You typed in wrong password or username, please try again mate!');
    window.location.href='music.php';
    </script>";

    }

    }
}?>

Any help more than appreciated but please try to keep it simple im stupid

4
  • After uploading pic successfully , you need to change the ` $_SESSION['userpic'] = $row->userpic;`variable value. Commented Feb 22, 2017 at 11:15
  • When User updating image... first you need to unlink existing image from folder and update session value with new image value and as HTML is already loaded then through ajax u need to update the image src with new one.....else till user will not logout or refresh image will not change. Commented Feb 22, 2017 at 11:15
  • it should be ` $_SESSION['userpic'] = $userpic;` when are you updating the users table. also unlink the old image as per @Naincy mentioned. Commented Feb 22, 2017 at 11:15
  • Thanks for all the answers guys! Really helped out! Commented Feb 22, 2017 at 11:23

3 Answers 3

6

When you upload the user image you update the content in the table to the associated user. However, you do not update the session variable with the corresponding value.

After you have ran the query successfully, before you return the success message, set the value of the session variable, like so:

[...]
$_SESSION['userpic'] = $userpic;
echo '<div id = "check"> Your image was succesfully uploaded</div>';
[...]

Edit: Note that the changing of the image will not happen on THIS pageload, it will happen after. This is because you are using the previous value up until this point.

It is a common approach to do a complete page load/redirect when you have finished a request. For example, you can store the output message in a session variable, redirect the user and then check if there are any messages to output.

Sample:

[...]
$sql->execute(); 
$sql->close();
$con->close();
$_SESSION['userpic'] = $userpic;
$_SESSION['messages'] = '<div id = "check"> Your image was succesfully uploaded</div>';
header("Location: index.php");

Then, somewhere in your index.php where you want to the message to be, you add something like this:

if (isset($_SESSION['messages']) and strlen($_SESSION['messages']) > 0) {
    echo $_SESSION['messages'];
    unset($_SESSION['messages']);
}
Sign up to request clarification or add additional context in comments.

2 Comments

he will update session still image will not change in real time till user will not refresh.
Oh thanks for the sample aswell! A really great answer and easy to understand!
1

You are not updating it in your session!.

Your session is populated at the time of login currently and you need to update $_SESSION['userpic'] = $userpic; in your upload script

Comments

1

It can be achieved by these steps when user modify the image

  • Upload new image
  • Successfully uploaded - update DB
  • Unlink old Image from folder
  • Update Session with new image data
  • If using ajax then return new src of img and manipulate DOM
  • Not using ajax then till user will not refresh the page it will not change.

Comments

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.