I am using CodeIgniter and I have two different controllers - login.php and site.php
In login.php
function credentials(){
$user = $this->input->post('username');
// there is some code here
}
In site.php, I have function members_area() I need to pass the variable $user from login.php to site.php. How do I do this ?
Thanks
If you are talking about user logins here.. In your Login controller you verify user credentials. If so then you need to set a session variable
$this->sessions->set_userdata('user_id',$user_id);
redirect('site/members_area');
Then in your Site controller you retrieve the data for that user from DB.
$current_user = $this->sessions->userdata('user_id');
And you get your required data from DB.
just take a redirect to site/members_area/$user
and you'll get $user parameter in the function members_area($user)
Turns out that a controller is used to control what is sent to a view. If the design is right you're not really supposed to send data between two controllers.
I sent inserted the value of username into a session.
So, In login.php
$this->session->set_userdata($data);
and retrieved the value in site.php
$data['userid'] = $this->session->userdata('username');
$this->load->view('members_area' , $data); //send the info to the controller
if you want some common functions in two controller put it in a helper file
http://ellislab.com/forums/viewthread/55212/
Related
i'm having issue getting session variable in a custom page like a controller.
For example if i create a controller like myController.php and i execute the #dd of data
$data = session::all();
dd($data);
I get no results.
But if i execute the same code in another page like homeController.php i retrieve everyhing the session have like user id etc. My question is how i can retrieve session variable in another page in laravel like i do in plain php with start_session() ?
Check that your route calling this controller you are working in, is under the web middleware, otherwise the session won't be shared.
I'm doing a project in php mvc but without framework. I organized the files in this way:
Directory public: there are files accessible from the outside
Directory View: there are files .php but essentially only html
Direcory Model: contains the files related to the database connection, and classes that define the objects, for example, the class User.php
Directory Controller: contains a 'generic' class Controller and Controller classes more 'specific'
I creat a login page (View/login.php) with a form whose action value is public/index.php. In index.php there is this code:
$controller = new LoginController();
$view = $controller->invoke();
$view->render();
invoke() is a function in Controller/LoginController.php that reads the data entered by the user and controls them, if they are correct (there is user in the database with the username and password) then creates two global variables and make a redirect:
$_SESSION['logged_in'] = 1;
$_SESSION['username'] = $username;
$url = "../public/home.php";
header("Location: $url", true, 302);
exit();
public/home.php does this:
$controller = new HomeController();
$view = $controller->invoke();
$view->render();
HomeController is a class that extends Controller. The constructor of Controller see if there are the variables $_SESSION['logged_in'] and $_SESSION['username'].
If they not exists it makes a redirect to public/index.php.
My problem is that the row with header("refresh:0; url=../public/home.php "); does not redirect.
Explain: when I insert the correct data (registered user) redirects for a short time but then returns to home.php.
Instead it should redirect to home.php and stay there, do not go to index.php .
I also tried with header("refresh: 0; url = ../public/home.php"); but it's the same problem.
How can I solve this problem?
Thank you and sorry for my poor English!
Add php ob_start() function on top of the page. If you call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.
Ref: http://in2.php.net/ob_start
cakephp is amazing but this auth stuff i haven't gotten all the pieces, my question is simple. I want to pass the current logged in users array data so i can use it in a view. I use animal names as my testing names
class UsersController extends AppController {
public function cat() {
$this->set('cat',$this->Auth->login($this->request->data) );
//I also tried $this->Auth->login()
}
}
now my view
//cat.ctp
<div>
<?php echo print_r($cat)?> //returns 11
</div>
Alternatively you can access user info via session helper in view file:
<?php print_r($this->Session->read('Auth.User')); ?>
In your controller, $this->Auth->user() will return you an array with current user data.
You can retrieve authentified user's data from the controller using
$this->Auth->user();
exp: $this->Auth->user('username');
I am using native PHP sessions ($_SESSION) with CodeIgniter framework.
I have a "Login" controller loads view where user enters login and password.
After the user submits the login form, the "Login" controllers authenticate() method is called.
If everything is alright i add some data to $_SESSION array, then i redirect user to "Organisation" controllers myOrganisation() method.
I'm calling session_start() in Login/login() , Login/authenticate() and Organisation/myOrganisation() methods, but still the session is not passed, because in myOrganisation() method the session is new.
I tested my cookies functionality with creating 2 test php pages, where i just echo session id. It works perfectly.
Maybe i am not putting session_start() in all places it needs to be? (i put them in all controllers methods).
Login Controller:
class Login extends CI_Controller {
public function index() {
session_start();
$this->load->view("Login/index", $data);
public function authenticate() {
session_start();
$_SESSION['login'] = $login; // everything is alright, redirect
header("location: ".base_url()."Organisations/MyOrganisation");
Organisation controller:
public function MyOrganisation() {
session_start(); // here session is a new one, not passed
if(isset($_SESSION['login'])) {
I don't know what was wrong with the session_start() placements that i did, but the one thing that solved the problem was to place it in index.php in main folder
session_start() can also be specified in a constructor, not every method in a class. That means both your controllers can have this:
function __construct () {
ini_set("session.gc_maxlifetime", 14400);
ini_set("session.cookie_domain", .yourdomain.com);
session_set_cookie_params(14400, '/', .yourdomain.com);
session_start();
}
The first 3 lines in a constructor are to make sure the session cookie is valid for a long time and under you domain.
Besides that (and not closed index() and authenticate() methods), where's $login coming from?
how to acess user session in sfDoctrineRoute with symfony ?
var_dump(sfContext::getInstance()->getUser());
returns NULL
i cant access current user session in routing
Accessing user session from a custom routing class = bad response
You should use the sfDoctrineRoute::setQuery() method from your controller, and generate a query using its sfUser reference and, for example, the user credentials it contains:
protected function executeIndex(sfWebRequest $request)
{
$query = Doctrine::getTable('Foo')
->createQuery('f')
->whereIn('f.access_level', $this->getUser()->getCredentials())
;
$this->getRoute()->setListQuery($query);
$this->foo_list = $this->getRoute()->getObjects();
}
Hope it helps.
PS: you should ALWAYS avoid calling sfContext::getInstance().