Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 426 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I print the username using session in cakephp?

#1
I cannot get my username to show on the website when I call it form the session.

The code on the index page is:

<h1>Users Home</h1>
Welcome <?php $user = $this->Session->read('Users.username');?>

What am I doing wrong?

I've also tried various other ways of calling it and then getting different error messages.
Reply

#2
In your code you are setting $user with the content of the username. But you are not printing it.

<h1>Users Home</h1>
Welcome <?=$this->Session->read('Auth.User.username')?>

which is short for

<h1>Users Home</h1>
Welcome <?php print $this->Session->read('Auth.User.username'); ?>
Reply

#3
After getting $user, you should do a echo $user or print_r($user) and you'll get the username printed.
Reply

#4
echo $this->Session->read('Auth.User.username');

Or if you are using Cakephp 2.x you can use `AuthComponent::user('username')` anywhere in your code.
Reply

#5
As Hugo said you should do the following:

<h1>Users Home</h1>
Welcome <?php echo $this->Session->read('Auth.User.username'); ?>


If you are going to use it on more than one view, I would suggest you to add the following on **AppController**.

public function beforeFilter() {
$this->set('username', AuthComponent::user('username'));
// OR $this->set('username', $this->Session->read('Auth.User.username'));
}

And then on any of your view just use the variable $username to print the current username.

<?php echo $username; ?>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through