Welcome Guest, Not a member yet? Register   Sign In
need to display username!
#1

[eluser]newbie boy[/eluser]
hi everyone!!! im new to this matter, hope you can help me with my problem. i'm still continiously learning and practicing the doctrine. how can i display the user's username to all succeding pages after the user successfully logged-in? hope you'll forgive my stupidity! thanks!!!

here's my codes:

for my controller:
<?php
class LoginController extends Controller {

function LoginController(){

parent::Controller();
$this->load->database();
$this->load->helper(array('url', 'form'));
$this->load->library(array('calendar','validation','session'));

}

function index(){

$isValid = true;

$username = $this->input->post('username');
$password = $this->input->post('password');

if (empty($username)) { $isValid = false; }
if (empty($password)) { $isValid = false; }

if ($this->session->userdata('logged_in') == '0') {

if ($isValid) {

$user = new UserModel();
$user = Doctrine_Query::create()
->select('u.*')
->from('UserModel u')
->where('u.user_id = ?', $username)
->fetchone();

$data['newUsername'] = $username;

$this->session->set_userdata('logged_in', '1');
$this->session->userdata('username');
redirect('HomeController', $data);

} else {

$this->load->view('userLoginView','refresh');

}
}
}
?>;

how do I pass the username to the other controller? So I can display them to the succeeding views? Thanks again!
#2

[eluser]OES[/eluser]
Session Data !.

http://ellislab.com/codeigniter/user-gui...sions.html

If you have a login/logout section to your site then set a users data while logging in. IE a simple model example.

Code:
//Check against user table
$this->db->where('email', $data['email']);
$query = $this->db->getwhere('users');
        
if ($query->num_rows() > 0):
  $row = $query->row_array();
  //Check against password
  if(md5($data['password']) != $row['password']) {
    return false;
  }
            
  $this->session->sess_destroy();        
  $this->session->sess_create();
  unset($row['password']);
  $member = array('user' => $row);
  $this->session->set_userdata($member);        
  //Login was successful            
  return true;
else:
  //No database result found
  return false;
endif;

Then you can collect your user details from within views or controller passing to a view ie using a controller method.

Code:
$this->data['user'] = $this->session->userdata('user');

Then within the view file. (As a smarty example)

Code:
{$user.username}

Hope this makes sense to you.
#3

[eluser]newbie boy[/eluser]
this one i can't solve yet! The solution must use the code igniter and doctrine. thanks!
#4

[eluser]manilodisan[/eluser]
What doctrine has to do with passing a username from one page to another? I have a strong feeling you're here with little to no PHP experience at all and all you need is someone to write your code. Do you know what doctrine does?
#5

[eluser]newbie boy[/eluser]
hey manilodisan, did you see my username. It actually read as "newbie boy". Hello. I hope you do know what the word "newbie" means!
#6

[eluser]newbie boy[/eluser]
and to this? . . .
#7

[eluser]newbie boy[/eluser]
i tried this but still doesn't display the logged user. anyone?

controller:
Code:
$this->load->library('session');
$newdata = array('email' => $login->email,'logged_in' => TRUE);
$this->session->set_userdata($newdata);
$session = $this->session->userdata('email');
$data['username'] = $this->session->userdata('email');
redirect('HomeProfileController', $data);

view:
Code:
<h3>Welcome &lt;?php echo $username; ?&gt;</h3>
#8

[eluser]CIfan1000[/eluser]
Hi,

In a redirect function, the 2nd parameter, in your case $data, cannot be data.
See the URL helper in the manual for this function.
You can pass your data by loading a view.
See Views in the manual.

Hope this helps.
#9

[eluser]lmv4321[/eluser]
[quote author="newbie boy" date="1225874181"]i tried this but still doesn't display the logged user. anyone?

controller:
Code:
$this->load->library('session');
$newdata = array('email' => $login->email,'logged_in' => TRUE);
$this->session->set_userdata($newdata);
$session = $this->session->userdata('email');
$data['username'] = $this->session->userdata('email');
redirect('HomeProfileController', $data);

view:
Code:
<h3>Welcome &lt;?php echo $username; ?&gt;</h3>
[/quote]

You should not redirect, you should load a view. In simple terms, a view is a webpage which will display the data that is passed to it from the controller. So, change the redirect line to this:
Code:
$this->load->view('viewFileName', $data);

Change viewFileName to whatever the name is of the view php webpage you create. It should be in the view folder.
#10

[eluser]newbie boy[/eluser]
It works now. Thank you for the advice guys.




Theme © iAndrew 2016 - Forum software by © MyBB