How do I print the username using session in cakephp? - php

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.

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'); ?>

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; ?>

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

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.

Related

How to store the list of data into session with single line in codeigniter

I want to store the list of data into session with single line and then how to extract that data on view.userData variable contains list of data.I can store the data like below that i know.but instead of writing multiple line can i store with single line.and how can i extract that data to use on view.Thanks in Advance.
$userData=$google_oauthV2->userinfo->get();
$this->session->set_userdata('userdata',$userData['id']);
$this->session->set_userdata('username',$userData['given_name'];
Can I store the data like below?
$this->session->set_userdata('userdata',$userData);
How can use the variable on view like this it is giving nothing
<?php if(!empty($userdata['given_name'])){?>
<li>HI <?php echo $userdata['given_name'];?></li>
<?php }
After reading the Codeigniter User Guide on sessions, where this is explained ( as we all have ), you could do the following...
This is demonstration code which is really good for testing stuff out like this.
The View auth_view.php for want of a better name
<h1> View </h1>
<?php if(!empty($this->session->given_name)){?>
<li>Hi <?= $this->session->given_name;?></li>
<?php }
The Controller
// $userData=$google_oauthV2->userinfo->get();
// Recreate the Array from google_oauthV2
$userData['id'] = 1;
$userData['given_name'] = 'Fred Flintstone';
$this->session->set_userdata($userData); // Save ALL the data from the array
var_dump($this->session->userdata()); // DEBUG- Lets look at what we get!
$data = $this->load->view('auth_view', NULL, true);
echo $data;
Does that help explain the possibilities?
I highly recommend reading the user guide, because I did to get this answer.
This is hard for me to answer as I don't know how the set_userdata() function works, ie will it accept an array.
But you could try:
$userData=$google_oauthV2->userinfo->get();
$this->session->set_userdata('data',$userData);
And on the view page:
<?php if(!empty($data['userData']['given_name'])){?>
<li>HI <?php echo $data['userData']['given_name'];?></li>
If that does not work this is what I would do.
$userData=$google_oauthV2->userinfo->get();
$this->session->set_userdata('userdata',$userData['id']);
$this->session->set_userdata('username',$userData['given_name']);
$_SESSION['data'] = $userData;
And on the view page:
<?php if(!empty($_SESSION['data']['userData']['given_name'])){?>
<li>HI <?php echo $_SESSION['data']['userData']['given_name'];?> </li>
<?php }
Also you are missing a right paren on:
$this->session->set_userdata('username',$userData['given_name'];
in your orginal code.
Yes, you can store data like this:
$this->session->set_userdata('userdata',$userData);
To access it from view, just store the session data into a local variable.
$userData = $_SESSION['userdata'];
then you can use it.
<?php if(!empty($userData['given_name'])){ ?>
<li>HI <?php echo $userData['given_name'];?> </li>
<?php } ?>
NOTE:
To use session in codeigniter you have to load the session library in your controllers.You can load it in constroctor or top of the method where you are going set session.(you don't have to load it to view.)
$this->load->library('session');

PHP syntax - shows text and not link

With this snippet of code, I'm attempting to show a clickable link (if "admin" is logged in), which will redirect me to adminarea.php
Right now it just prints out "Admin" in text. Nothing to click on. Just simple text.
Am I missing anything? Surely I got it wrong but I cannot see what's missing.
Here is the code:
<?php if (getUser("user") == "admin") { ?>
<option value="adminarea.php">Admin</option>
<?php } ?>
You're printing an option, which is part of the select form input. You're probably looking for an anchor?
Admin
Possibly a better way to do this would be to declare two options for a variable in your PHP first. Something like:
<?php
if(getUser("user") == "admin") {
$adminlink = 'Admin';
} else {
$adminlink = NULL;
}
?>
And in the html:
<?php echo $adminlink; ?>
This would show the href link if the PHP condition was true, and would display nothing if not. Hope this helps!
Well based on your title am assuming you want a link. By the way you can use PHP friend html syntax instead of making the code look "dirty".
<?php if(getUser("user") == "admin"): ?>
Admin
<?php endif; ?>

How to use $GLOBALS to share variables across php files?

I have a file, index.php that produces a link to a page that I want my user to only be able to access if some $var == True.
I want to be able to do this through the $GLOBALS array, since my $_SESSION array is already being filled with instances of a specific class I want to manipulate further on.
My index.php page:
<?php
$var = True;
$GLOBALS["var"];
echo "<p><a href='next.php'>Click to go to next page</a></p>";
?>
My next.php page:
<?php
if($GLOBALS["var"] == False)
exit("You do not have access to this page!");
else
echo "<p>You have access!</p>";
?>
Currently, next.php is echoing the exit text. Am I accessing/assigning to the $GLOBALS array correctly? Or am I not using it properly?
Thanks!
EDIT:
So I've tried some of the suggestions here. This is my new index.php:
<?php
$GLOBALS["var"] = True;
echo "<p><a href='next.php'>Click to go to next page</a></p>";
?>
My next.php:
<?php
if($GLOBALS["var"] == False)
exit("You do not have access to this page!");
else
echo "<p>You have access!</p>";
?>
However, I'm still running into the same issue where the exit statement is being printed.
It's much better to use sessions for this, since they are more secure and exist for this purpose. The approach I would recommend, is starting a new separate session array.
session_start();
$_SESSION['newSession']['access'] = true;
Then to access it use the same key/value.

Fetching text value from SQL table to use in header: Location

I want to fetch the landing_page value from this SQL table and add it to header location
: Ive got the following code at the top of the page but this is giving me a blank page:
<?php
foreach($[user_name] as $user) {
$location="Location:http://".$user['landing_page'];
header($location);
}
?>
<!-- if you need user information, just put them into the $_SESSION variable and output them here -->
Order: <?php echo $_SESSION['user_name']; ?>. You are securely logged in.
Where am i going wrong?
The original code is a login script with me trying to make the user to go google.com once logged in
You are not appending the variable properly. Try with,
header('"'.$user['landing_page'].'");
I think you can do this by Simply appending the argument in header
header("Location: " . $user['landing_page']);
try this:
<?php
header("Location: ".$user['landing_page']);
?>
Try this
<?php
foreach($user_name as $user) {
header("Location:http://".$user['landing_page']);
}
?>
or
<?php
foreach($user_name as $user) {
$location="Location:http://".$user['landing_page'];
header($location);
}
?>

How to assign posted values to a variable in cakephp?

I likes to assign posted values to a variable,
In view i wrote the code,
<?php echo $this->Form->create('Register'); ?>
<fieldset>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('Confirm password');
?>
</fieldset>
Form->end(__('Submit'));
and in controller,
if($this->request->is('post')){
//print_r($this->data);
$uname = $this->request->data('username');
echo "uname".$uname;
exit;
}
The problem is that i didnt get the value on $uname.
Make sure your debug is enabled. Use this - pr($this->request->data); to see what's in that variable. The CakePHP form naming data usually looks something like this
$this->request->data['Formname']['fieldname']
In your case it should be
$this->request->data['Register']['username']
please try using this code
$this->params['form']['YOUR_VARIABLE_NAME']
or
$this->data['FORMNAME']['YOUR_VARIABLE_NAME']

Categories