In a controller, I have my method post_sessions(), and I save some variables:
session_start();
$_SESSION['sessions'] = $sessions;
$_SESSION['sessions2'] = $sessions2;
session_write_close();
This method works fine, if I make a return of $_SESSION['sessions'] it will show my variable.
The problem is when I want to get the session back, in other function I have this:
session_start();
$var = $_SESSION['sessions']
But if I make this, I get error 500...
I have put in all htmls... but it doesn´t work. Also I tried with Session::put('key', 'value') of Laravel 5, but it doesn´t work too.
Someone can helpme?
Related
I´m trying now for days saving data in a session to use for view contextual filter etc.
The user should select something (1,2,3,4) from a dropdown, saved in a post variable and write into session.
I tried several methods, but none is working.
$tempstore = \Drupal::service('user.private_tempstore')->get('mymodule_name');
$tempstore->set('my_variable_name', $some_data);
from another post, put in a module ending in an
"Fatal error: Call to a member function getSession() on null in
/usr/www/users/smplce/nordisch/core/modules/user/src/PrivateTempStore.php
on line 210"
Putting this in a module:
$_SESSION['area_session']['area'] = "test";
Is not causing an error, but trying to output in page.html.twig like this:
{{ area_session.area }}
It's doing nothing.
Please help me, I´m not really a programmer but want to solve this problem.
Thanks in advance!
In Drupal 7, you would access GET, POST, SERVER and COOKIE variables using the PHP super-globals $_GET, $_POST, $_SERVER and $_COOKIE.
Examples:
-> Drupal 7:
$query = $_GET['q']; // query string param
$myparam = $_POST['myparam']; // form param
$request_method = $_SERVER['REQUEST_METHOD']; // server variable
$mycookie = $_COOKIE['mycookie']; // cookie
In Drupal 8, you need to use the Symfony Request object.
->Drupal 8:
$query = \Drupal::request()->query->get('q'); // query string param
$name = \Drupal::request()->request->get('name'); // form param
$request_method = \Drupal::request()->server->get('REQUEST_METHOD'); // server variable
$mycookie = \Drupal::request()->cookies->get('mycookie'); // cookie
How do you set a $GET variable?
Hi i have done this before, but my method is a bit hit and miss so i was wondering what is the correct way to do this.
My url is:
Franchise-Details.php?Franchise=Enfield/status=Driver
And i want to set the Franchise name and staff status as a variable as i will be using them continuously throughout my webpage.
This is what i have done:
$admin_status = mysqli_real_escape_string($dbc,$_GET['status']);
$fr_Area = mysqli_real_escape_string($dbc, $_GET['Franchise']);
But it does not work. No error messages, just shows nothing if i try to apply it, for example echo $fr_area;. If i change the get to session it works. But i want to use get.
I have also tried:
if (isset($_GET['status'])){
$admin_status = mysqli_real_escape_string($dbc,$_GET['status']);
}
if (isset($_GET['Franchise'])){
$admin_status = mysqli_real_escape_string($dbc,$_GET['Franchise']);
}
How can i improve this
I have a function on my page to login it works like this:
function entrarSistema($email,$senha){
if(isset($email) and (autentica($email,$senha)!=false)){
$mysqli = connect_db();
$result = mysqli_query($mysqli,"SELECT ID FROM px_user WHERE email = '$email'");
$id = mysqli_fetch_array($result);
$_SESSION['nome'] = autentica($email,$senha);
$_SESSION['email'] = $email;
$_SESSION['password'] = $senha;
$_SESSION['ID'] = $id[0];
$_SESSION['logado'] = true;
}
else{
if(check_double($email)==1){
setCodeAlerta(1);
echo $_SESSION['status'];
}
else {
setCodeAlerta(2);
}
}
}
This function works fine, and the Session variables are set when i call the function setCodeAlerta(), the Session variable i've declared wont work. Notice that this 2 functions are on the SAME file Here is the function:
function setCodeAlerta($numeroCodigo){
$_SESSION['status'] = $numeroCodigo;
}
My index.php has all the pages included, and i use url_rewrite to add the piece of codes that i need, and has this on its very top:
if( !isset($_SESSION) ){ session_start(); }
Oddly enough, if i call directly a file named test.php with this code:
<?php
setCodeAlerta(2);
?>
The variable is set fine, and everything works well.
Thanks in advance.
I'd add a comment, but my rep isn't high enough yet. Out of curiosity are the other session vars available at that point that you are setting? -- I ask because I'm running your code (stripped down) and it's returning OK. I'm just thinking there's something more to this than just that. Are you calling session_start() twice anywhere in an include or anything?
I got that. Everything was absolutely fine with my code, i just did a conditional, where my code would never really go to wrong username/password, hence, not accessing my function (wich is working fine). After removing the second part (after the and) part of this conditional, my code worked just fine.
if (isset($_POST['submitLogin']) and autentica($_POST['emailLogin'],$_POST['passwordLogin']){
entrarSistema($_POST['emailLogin'],$_POST['passwordLogin']);
}
I normally ask questions and find my answer minutes after that, i think thats a bad habit of mine. Even so, i spent the last 5 hours debugging it.
Thanks!
I have a website where the front page contains a search form with several fields.
When the user performs a search, I make an ajax call to a function in a controller.
Basically, when the user clicks on the submit button, I send an ajax call via post to:
Route::post('/search', 'SearchController#general');
Then, in the SearchController class, in the function general, I store the values received in a session variable which is an object:
Session::get("search")->language = Input::get("language");
Session::get("search")->category = Input::get("category");
//I'm using examples, not the real variables names
After updating the session variable, in fact, right after the code snippet shown above, I create (or override) a cookie storing the session values:
Cookie::queue("mysite_search", json_encode(Session::get("search")));
And after that operation, I perform the search query and send the results, etc.
All that work fine, but I'm not getting back the values in the cookie. Let me explain myself.
As soon as the front page of my website is opened, I perform an action like this:
if (!Session::has("search")) {
//check for a cookie
$search = Cookie::get('mysite_search');
if($search) Session::put("search", json_decode($search));
else {
$search = new stdClass();
$search->language = "any";
$search->category = "any";
Session::put("search", $search);
}
}
That seems to be always failing if($search) is always returning false, and as a result, my session variable search has always its properties language and category populated with the value any. (Again: I'm using examples, not the real variables names).
So, I would like to know what is happening here and how I could achieve what I'm intending to do.
I tried to put Session::put("search", json_decode($search)); right after $search = Cookie::get('mysite_search'); removing all the if else block, and that throws an error (the ajax call returns an error) so the whole thing is failling at some point, when storing the object in the cookie or when retieving it.
Or could also be something else. I don't know. That's why I'm here. Thanks for reading such a long question.
Ok. This is what was going on.
The problem was this:
Cookie::queue("mysite_search", json_encode(Session::get("search")));
Before having it that way I had this:
Cookie::forever("mysite_search", json_encode(Session::get("search")));
But for some reason, that approach with forever wasn't creating any cookie, so I swichted to queue (this is Laravel 4.2). But queue needs a third parameter with the expiration time. So, what was really going on is that the cookie was being deleted after closing the browser (I also have the session.php in app/config folder set to 'lifetime' => 0 and 'expire_on_close' => true which is exactly what I want).
In simple words, I set the expiration time to forever (5 years) this way:
Cookie::queue("mysite_search", json_encode(Session::get("search")), 2592000);
And now it seems to be working fine after testing it.
In my project, I am using pagination and I used these statements to get the page number detail:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
When I print this session value in that page itself, I get the value correctly and when I click on the particular link and then print that data, I am getting the value like 'images'.
Why is this happening?
However, when I write the statements like
$page=$this->uri->segment(2);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
it's working fine.
My URL is: http://localhost/CI/user/index/4
I have the same problem with you, the session variable always get value 'images'. Then i realize that: if we made a session variable, then we must call redirect() directly.
This is my example:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
redirect('control/function2');
Then, you can get continue your code in the 'function2'.
Solved the issue by writing another function, setting the session and redirecting to above function like this:
function set_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
redirect('set_uri_session');
}
function set_uri_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
echo $this->session->userdata('id');
}