I use Gethebert (http://getherbert.com/) as plugin in wordpress. This one is having structure as Laravel framework.
Also it uses mostly same components as laravel used.
But my query is how to use its session.
In laravel i use,
Session::put('name','value'); //to Set Session Value
and
Session::get('name'); //to Get Session Value
But in Getherbert, I dont know the right way to use session.
Suggest me the right one....
Thank you !
You can always use standard PHP $_SESSION:
http://php.net/manual/en/session.examples.php
For example to put:
$_SESSION['name'] = $value;
And to get:
$value = $_SESSION['name'];
Below you can find , how to use session in Laravel.
Setting a single variable in session :-
syntax :- Session::put('key', 'value');
example :- Session::put('email', $data['email']); //array index
Session::put('email', $email); // a single variable
Session::put('email', 'test#test.com'); // a string
Retrieving value from session :-
syntax :- Session::get('key');
example :- Session::get('email');
Checking a variable exist in session :-
// Checking email key exist in session.
if (Session::has('email')) {
echo Session::get('email');
}
Deleting a variable from session :-
syntax :- Session::forget('key');
example :- Session::forget('email');
Removing all variables from session :-
Session::flush();
Source : http://tutsnare.com/how-to-use-session-in-laravel/
Thank to all...
Finally i got answer for using session in Getherbert[laravel],
here it is,
use Herbert\Framework\Session;
class Admin extends MyCore{
function index(){
session()->set('age',24);
dd(session()->get('age'));
}
}
OUTPUT :
24
Related
I need to pass information between pages.
I would usually just use sessions in plain PHP. I wonder if there is a more correct way to do this on Laravel.
First_page.php
<?php
session_start();
$_SESSION["ID"] = 'item_id';
?>
Second_page.php
<?php
session_start();
echo $_SESSION['ID'];
#"item_id"
?>
How is this supposed to be done in Laravel?
Just use Session.
Session::flash(); //or Session::put
Session::flash('values', $passvalues);
Redirect::to('/add1');
$oldinput = Session::get('values');
In laravel you can pass a variable in your url i.e. :
In routes.php
Route::get('/page1', 'YourController#Yourfunction');
Route::get('/page2/{id}', 'YourController#loadpage2');
In your controller.php
public function Yourfunction(){
return view('page1');
}
public function loadpage2(id){
return view('page2', compact('id'));
}
in your view have a link to url /page2/3 where 3 is your id
If you want to use session then you need to do 2 things.
save session variables in your controller
display session variables in your view
In your controller you can use something like
session()->put('your_session_variable','some value');
In your view then you can display that session variable
#if(session()->has('your_session_variable'))
{{Session::get('your_session_varible',"session_not_set(default value)") }}
#endif
Its good to use default value for displaying session variables (many times that session variable is not set).
I would like to set a variable in the session using laravel this way
Session::set('variableName')=$value;
but the problem is that I don't know where to put this code, 'cause I would like to set it for one time (when the guest visite the home page or any other page)?
The main idea is to use a global variable to use it in all application controllers, I heared about something related to configuration variables but I'm not sure if it will be a good Idea to use config variables or only the session?
Thanks
The correct syntax for this is:
Session::set('variableName', $value);
For Laravel 5.4 and later, the correct method to use is put:
Session::put('variableName', $value);
To get the variable, you would use:
Session::get('variableName');
If you need to set it once, I'd figure out when exactly you want it set and use Events to do it.
For example, if you want to set it when someone logs in, you'd use:
Event::listen('auth.login', function() {
Session::set('variableName', $value);
});
I think your question ultimately can be boiled down to this:
Where can I set a long-lived value that is accessible globally in my application?
The obvious answer is that it depends. What it depends on are a couple of factors:
Will the value ever be different, or is it going to be the same for everybody?
How long exactly is long-lived? (Forever? A Day? One browsing 'session'?)
Config
If the value is the same for everyone and will seldom change, the best place to probably put it is in a configuration file somewhere underneath app/config, e.g. app/config/companyname.php:
<?php
return [
'somevalue' => 10,
];
You could access this value from anywhere in your application via Config::get('companyname.somevalue')
Session
If the value you are intending to store is going to be different for each user, the most logical place to put it is in Session. This is what you allude to in your question, but you are using incorrect syntax. The correct syntax to store a variable in Session is:
Session::put('somekey', 'somevalue');
The correct syntax to retrieve it back out later is:
Session::get('somekey');
As far as when to perform these operations, that's a little up to you. I would probably choose a route filter if on Laravel 4.x or Middleware if using Laravel 5. Below is an example of using a route filter that leverages another class to actually come up with the value:
// File: ValueMaker.php (saved in some folder that can be autoloaded)
class ValueMaker
{
public function makeValue()
{
return 42;
}
}
// File: app/filters.php is probably the best place
Route::filter('set_value', function() {
$valueMaker = app()->make('ValueMaker');
Session::put('somevalue', $valueMaker->makeValue());
});
// File: app/routes.php
Route::group(['before' => 'set_value'], function() {
// Value has already been 'made' by this point.
return View::make('view')
->with('value', Session::get('somevalue'))
;
});
In Laravel 5.6, you will need to set it as:
session(['variableName' => $value]);
To retrieve it, is as simple as:
$variableName = session('variableName');
For example, To store data in the session, you will typically use the putmethod or the session helper:
// Via a request instance...
$request->session()->put('key', 'value');
or
// Via the global helper...
session(['key' => 'value']);
for retrieving an item from the session, you can use get :
$value = $request->session()->get('key', 'default value');
or global session helper :
$value = session('key', 'default value');
To determine if an item is present in the session, you may use the has method:
if ($request->session()->has('users')) {
//
}
in Laravel 5.4
use this method:
Session::put('variableName', $value);
To add to the above answers, ensure you define your function like this:
public function functionName(Request $request) {
//
}
Note the "(Request $request)", now set a session like this:
$request->session()->put('key', 'value');
And retrieve the session in this way:
$data = $request->session()->get('key');
To erase the session try this:
$request->session()->forget('key');
or
$request->session()->flush();
You can try
Session::put('variable_Name', "Your Data Save Successfully !");
Session::get('variable_Name');
In Laravel 6.x
// Retrieve a piece of data from the session...
$value = session('key');
// Specifying a default value...
$value = session('key', 'default');
// Store a piece of data in the session...
session(['key' => 'value']);
https://laravel.com/docs/6.x/session
If you want persistence sessions,
Method 1: use session()->save() or Session::save()
session(['key' => 'value']);
//or
session()->put('key', 'value');
//then
session()->save();
echo session('key');
Method 2: Move bellow line from protected $middlewareGroups of app\Http\Kernel.php to protected $middleware array as first line
\Illuminate\Session\Middleware\StartSession::class,
Make sure the storage directory has write permission
chmod -R a+rw storage/
Don't use dd() to verify session, use print_r()
to set session you can try this:
$request->session()->put('key','value');
also to get session data you can try this:
$request->session()->get('key');
If you want to get all session data:
$request->session()->all();
I need to get session_id() in ZF2? Using session_id() won't return any value. I tried Zend_Session::getId() but it didn't work, probably because I haven't included the path (which I don't know).
Can anyone help me to find a solution. Thanks in advance.
session_id() should work, but the correct way would be to access it from the session manager, e.g. (from a controller):
$sessionManager = $this->getServiceLocator()->get('Zend\Session\SessionManager');
$id = $sessionManager->getId();
If neither of these are returning a value for you then there's another problem somewhere.
first of all you have to use zend session container in your page that is.
use Zend\Session\Container;
now create an object of container by following.
$session = new Container('User');
now set value $userid or any other variable in session by following object.
$session->offsetSet('userId', $id);
now you can get it by following code.
$user_id=$session->offsetGet('userId');
Experts,
i have a little Problem with my sessions. I want to save my login data into the session like this:
checklogin Controller
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
var_dump($this->session->set_userdata($user)); // return NULL? is this correct?
var_dump($this->session->all_userdata()); //return the correct data
Now the Session is saved up to a redirect.
Other Controller
var_dump($this->session->all_userdata()); // return session_id,… and a empty user_data array
I think I have the same Problem with the Shopping Cart Class.
Can anyone help?
I dont know why, but i think my story will help someone who facing the same problem.
My problem is just because I forgot to downgrade my Wamp php version, its php 7.1. Looks like my CI not support php 7.1 or higher. So just a few click to downgrade my php, my problem solved.
I added session_start() in the main index.php file. Worked like a charm afterwards.
try this..
$user = $this->user_model->user($email, $password);
$user["logged_in"] = TRUE;
$this->session->set_userdata('user',$user); //set session of users with a name user.
to get the session value u can do..
print_r($this->session->userdata('user')); // prints the user session array..
read more about sessions in CI
The set_userdata() function does not have a return value, so it is correct to display null on var_dump().
In order to save custom data to user's session you should check the manual here
The parameter it accepts must be an array.
hmm try add to ci->session and then get data
//assign the CodeIgniter object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
//add data to ci session
function test(){
$this->ci->session->set_userdata($user)
}
in the other controller assign the CodeIgniter object to a variable
function __construct() {
parent::__construct ();
$this->ci = & get_instance ();
}
and get the ci user data
$this->ci->session->all_userdata()
I got a similar issue when I want to rerun an old website that was built formerly using CI 3.0.6 on a server running PHP 7.3.
I have set a session in one controller.
$this->session->set_userdata('logged_in', TRUE);
Then, the other controllers that are accessed after that cannot retrieve the information while a new record in the session table in the database is stored.
empty($_SESSION['logged_in']); // true
empty($this->session->logged_in); // true
The solution that works for me is as follows.
Set a higher size for the id column in the CI session table. For example, our session table name is ci_sessions.
ALTER TABLE ci_sessions CHANGE id id varchar(128) NOT NULL;
Replace the current CI system directory with newer version. In my case, it works with the system directory from CI 3.1.10.
In summary, a newer CI version is required to be able to interact well with newer PHP (7.1+). It has a longer id value for the stored session record. Rather than downgrading your PHP version, it is better to upgrade the CI version.
I have a form which is passing a number array like (56,78,98). I have stored that array in a variable called $array.
eg:
if(isset($_POST['submit']))
{
$checkbox_values=$_POST['checkbox_values];
$array= implode(',',$checkbox_values);
}
$_POST['checkbox_values] is values of check boxes from a form.
I need to retrieve value of $array after the if(isset($_POST['submit'])) condition. I am using zend frame work. How can I use $array after if condition? or how can I use session or cookie in zend?
Thanks!!!
you could assign the array directly to the session using Zend_Session_Namespace:
//you can initialize the session namespace almost anywhere.
$session = new Zend_Session_Namespace('form')
if(isset($_POST['submit'])) {
$checkbox_values=$_POST['checkbox_values'];
//will assign values to session namespace 'form' as key 'checkbox_values' as an array
//session namespace will also accept objects and scalar values.
$session->checkbox_values = implode(',', $checkbox_values);
//$array= implode(',',$checkbox_values);
}
you can use the array any way you choose. You can pass it to the view...
$this->view->checkboxValues = $session->checkbox_values;
or you can pass it to your favorite model, what ever you need to do you can do. the session will remain until you unset it or overwrite it.
Good Luck.
you are missing a single quote
$checkbox_values = $_POST['checkbox_values'];
otherwise everything looks fine.