$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->AccountModel->login($username,$password);
if($result){
foreach($result as $row){
$loggedin = array('Admin_name' => $row['username'], 'is_loggedin' => TRUE);
$this->session->set_userdata($loggedin);
How would i be able to access ['Admin_name'] and ['is_loggedin'] without having to run this code and running a foreach loop $this->session->all_userdata()
From "Retrieving Session Data" section of Session Class docs:
Any piece of information from the session array is available using the following function:
$this->session->userdata('item');
So you want:
$this->session->userdata('Admin_name');
The docs are your friend ;)
try this
$this->session->userdata('Admin_name');
$this->session->userdata('is_loggedin');
You can access as below:
$Admin_name = $this->session->userdata('Admin_name');
For session library, please check the CI User guide on sessions
u can access it by using this line
$this->session->userdata['Admin_name'];
it should work ..if it dosen't ..just print the session variables using any of the line below
echo '<pre>';print_r($this->session->all_userdata());exit;
echo '<pre>';var_dump($this->session->all_userdata());exit;
once u see the structure of userdata ..u can easily access any session variables in there just like any other array ..just use
$this->session->userdata['array_key'];
Related
I've got a problem with a SESSION, inside a function.
How can I set $_SESSION['idUser'] = $result['idUser']; to a SESSION, which I can use for upcoming activity's.
<?php
// Session starts
session_start();
class DB_Functions {
public function getUserByEmailAndPassword($email, $password) {
$result = mysql_query("SELECT * FROM User WHERE emailadress = '" . $email . "'") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
//This is the session I want to use
$_SESSION['idUser'] = $result['idUser'];
return $result;
Assuming this is just a snippet of a larger class, I see no reason why this line:
$_SESSION['idUser'] = $result['idUser'];
wouldn't work (again assuming that $result['idUser'] contains a value). Ensure that you have session_start(); called on all pages that you want to use the global session variable.
I suggest using the following in the first place:
Session Management Library
I highly recommend it.
Like you wanted, you could use it everywhere.
It'll keep you from using super globals like $_SESSION (not easy to debug)
Make your code more testable and it'll alleviate you from having to manage sessions manually.
Example code:
use Symfony\Component\HttpFoundation\Session\Session;
$session = new Session();
$session->start();
// set and get session attributes
$session->set('name', $result['id_user']);
$session->get('name'); //Easy to get 'id_user' in another page.
//Other examples:
// set flash messages (appears only once, for warnings/alerts)
$session->getFlashBag()->add('notice', 'Profile updated');
// retrieve messages
foreach ($session->getFlashBag()->get('notice', array()) as $message)
{
echo '<div class="flash-notice">'.$message.'</div>';
}
If you like like what you see here are the links:
Download
Install Instructions
Warning
Make sure your PHP session isn't already started before using the
Session class. If you have a legacy session system that starts your
session, see Legacy Sessions.
Delete the space before <?php. Before session_start() there shouldn't be any output.
How to post values to loginchk_coustomer.php given in below code, not through Url by any other way.
Is there any other way to post these value to loginchk_coustomer.php becoz it is not secure.
<?php
include "include/connect.php";
$user_name = $_REQUEST['user_name'];
$password = $_REQUEST['password'];
//echo "select * from school_info where school_id='$user_name' and school_password='$password'";
$sql_query = mysql_fetch_assoc(mysql_query("select * from school_info where school_id='$user_name' and school_password='$password'"));
$db_username = $sql_query['db_username'];
$db_password = $sql_query['db_password'];
$db_databasename = $sql_query['db_databasename'];
echo "<script>";
echo "self.location='member/loginchk_customer.php?db_username=$db_username&db_password=$db_password&db_databasename=$db_databasename&user_name=$user_name&password=$password'"; // Comment this line if you don't want to redirect
echo "</script>";
?>
You need to create a session to store all that information.
Here's what they are - from http://php.net/manual/en/features.sessions.php:
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
To start a session write at the beginning of your code:
session_start(); // needed in all pages that will use the variables below
and then after your assign the information this way:
$_SESSION['username'] = $sql_query['db_username'];
$_SESSION['password'] = $sql_query['db_password'];
$_SESSION['databasename'] = $sql_query['db_databasename'];
All the information will persist on those variables along the site until you do:
session_destroy();
I also recommend you not to redirect with javascript, but this way in PHP:
header('Location: member/loginchk_customer.php');
Possibly after checking this answer you will think about to change the way you check the login information. But that's okay. It's the way of learning.
More information about sessions: http://php.net/manual/en/book.session.php
I hope this helps.
I was wondering how I would go about getting the user's username. What i want to do is display it like this:
Username here
I tried this:
{$mybb->user['name']}'
But that was unsuccessful and I cannot seem to find anything on Google.
Thanks for any help!
I am not that experienced with MyBB but after some research i found a few different ways.
$user = get_user($uid);
echo $user['username'];
or
global $db;
$qry = $db->query("SELECT uid FROM ".TABLE_PREFIX."users WHERE username = '".$usernamevar."'");
And you can combine all like this, i think.
<?php
define("IN_MYBB", 1);
require ('global.php'); // be sure that u r running this php-code in the same
// directory of global.php, or else change the path.
if($mybb->user['uid'] > 0)
{
$uid = $mybb->user['uid'];
$user = get_user($uid);
$name = $user['username'];
}
// custom else here: in case of not logged-in user
?>
<?echo $name?>
Try to put this one into your template.
{$mybb->user['username']}
No need to use PHP for an existed variable.
I have got a basic login script at the moment.
When the user logs in 2 variables are defined:
$_SESSION['user_id'] = $user['user_id'];
$_SESSION['username'] = $user['username'];
the index.php file has this
session_start();
if(array_key_exists('user_id', $_SESSION) && array_key_exists('username', $_SESSION)):
That is all fine, however once the session is started I would like to add more values from a database so I have this code here:
$res = mysql_query($sql);
$_SESSION = mysql_fetch_assoc($res);
When I do this it overrides the $_SESSION['user_id'] and $_SESSION['username'].
I tried this:
$_SESSION .= mysql_fetch_assoc($res);
but it didn't work.
Does anyone have any ideas?
Thanks
peter
$_SESSION is an array. You are over-riding the entire $_SESSION variable. Do this instead:
$_SESSION['data'] = mysql_fetch_assoc($res);
http://us2.php.net/manual/en/session.examples.basic.php
That's because you're setting the value of the variable $_SESSION to the return value of mysql_fetch_assoc($res);.
What you want to do is something like $_SESSION['query_result'] = mysql_fetch_assoc($res). If you just want to add a single column of your database result to the session you would do it like this:
$res = mysql_query($sql);
$data = mysql_fetch_assoc($res);
$_SESSION['myKey'] = $data['myKey'];
Try merging the arrays
$res = mysql_query($sql);
$_SESSION = array_merge(mysql_fetch_assoc($res), $_SESSION);
See http://php.net/manual/en/function.array-merge.php
Functions mysql_* are deprecated and removed in the future, use PDO or MySQLi is recommended.
Some hosters have disabled these functions.
I'm trying to set a session variable and use it on another page.
I have: pg1
session_start();
$_session['sessionID'] = $row['ID'];
Then on page two I have.
session_start();
$userID = $sessionID;
But when I use JC to alert this out I get nothing.
Am I doing this wrong?
Rather than:
$userID = $sessionID;
Use:
$userID = $_SESSION['sessionID']
You need to specify the $_SESSION there because that is the array you stored the value in :)
Have a look at this session tutorial if you want.
On the second page, you'll need to say
$userID = $_SESSION['sessionID'];
You need to do the following on page 2:
session_start();
$userID = $_SESSION['sessionID'];
You also need to use $_SESSION, not $_session