John Davidson

php - session->getFlashData shows more than once on view

0 comments
Message:


I am using codeigniter4 and uses the following code


In the controller -


$this->session->setFlashdata('success','Your  Data is saved'); 
return view('dashboard');

In the View


 <?php if (session()->get('success')): ?>
<div class="alert alert-success" role="alert">
<?= session()->getFlashdata('success'); ?>
</div>
<?php endif; ?>
</div>

As per code igniter 4, this flashdata message should clear. But the flashdata shows in the second page also. Then it clears on the third page.



My Answer:
The session in PHP is a way to store information across multiple pages of a website. It allows you to store variables that can be accessed and manipulated throughout the user's visit to the site. Sessions are stored on the server and are identified by a unique session ID that is passed between the server and the client's browser.

To start a session in PHP, you can use the `session_start()` function at the beginning of your script. This will create a new session or resume an existing one if one already exists.

You can store variables in the session using the `$_SESSION` superglobal array. For example, to store a user's name in a session variable, you can do:

php
$_SESSION['username'] = 'JohnDoe';


To access this variable on another page, you can simply use `$_SESSION['username']`.

Remember to call `session_start()` at the beginning of each script where you want to access session variables. You can also use `session_destroy()` to destroy the current session and unset all session variables.

It's important to note that sessions are stored on the server and can be manipulated by the user if not properly secured. Be sure to validate and sanitize any user input before storing it in a session variable to prevent security vulnerabilities.

Rate this post

4 of 5 based on 4312 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us