kunal kunal - 1 month ago 8
PHP Question

session becomes empty when accessed in another function

I am using laravel framework 5.2. I am using dropzone for multiple images. The all images are successfully stored in session. Now problem is that when i accessed that particular session in another function it gives me blank result here is my balde file and function

enter code here
<---Dropzone-->
<div class="modal-body">
<div class="dropzone-img">
<form id="my-awesome-dropzone" action="{{ url('/add-images')}}" class="fallback dropzone">
{{ csrf_field() }}

</div>
</div>


Now my controller having two functions

enter code here
public function add_images(Request $request){
if($request->ajax()){
$image=$_FILES['file'];
if(!empty($image)){
if($image['error']==0){
$imgName = pathinfo($_FILES['file']['name']);
$ext = $imgName['extension'];
$rand=mt_rand(5,1000000000).'.'.$ext;
$destination=realpath('../public/images/ListingImages').'/';
if(move_uploaded_file($image['tmp_name'], $destination.$rand)){
$request->session()->put('businessimages.'.$imgName['filename'].'.image',$rand);
$images = $request->session()->get('businessimages');
echo "<pre>"; print_r($images); die;
}
}
}
}
}

Now when i accessed the businessimages session in another function it gives me empty result.

public function business_for_sale(Request $request){
$images = $request->session()->get('businessimages');
echo "<pre>"; print_r($images); die;
}


Can anyone help me. Thanks in advance:)

Answer

I think the problem is for you call the die() function when you set the session

$request->session()->put('businessimages.'.$imgName['filename'].'.image',$rand);
$images = $request->session()->get('businessimages');
echo "<pre>"; print_r($images); die;

You could see it Does die() end your session in PHP?

So you could try to change it like

$request->session()->put('businessimages',$rand);
$images = $request->session()->get('businessimages');
echo "<pre>"; print_r($images); 
session_register_shutdown();
die;