Adding variables to $_SESSION - php

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.

Related

How to create session for login in core php?

i used this query to login and for creating session.
$username = $_REQUEST['user'];
$password = $_REQUEST['pass'];
$member = mysqli_query($conn,"SELECT * FROM `user` WHERE `email` = '".$username."' AND `password` = '".$password."' ");
$member1 = mysqli_fetch_assoc($member);
if ($member1>0)
{
$_SESSION['username']=$member1['fullName'];
$_SESSION['companyid']=$member1['comapanyId'];
header('Location: home.php');
}
else
{
header('Location: index.php');
}
i am able to create session means if i echo
$_session['companyid'];die; on this page it will print that id
perfectly.
Now, i will jump to home.php to use this session for that i write
session_start() on top of the page.
Now, If i print_r($_SESSION['companyid']) i will get error Notice:
Undefined index: companyid
for this problem i used isset function like this,
if (isset($_SESSION['companyid'])) {
echo $_SESSION['companyid'];
}
but i am again failed to print $_SESSION in home.php I dont know what
i am doing wrong.
i am able to create session means if i echo
$_session['companyid'];die; on this page it will print that id
perfectly.
The array $_session is different than the superglobal array $_SESSION due to case sensitivity. On top of that, you will need session_start() at the top of each PHP page that will be accessing this array.
If you change $_session to $_SESSION, PHP will know that you are calling the same array.

Why Does the location part dont work?

Index.php
$password = hash('sha256', $pass); // password hashing using SHA256
$res=mysql_query("SELECT * FROM users WHERE userEmail='$email'");
$row=mysql_fetch_array($res);
$count = mysql_num_rows($res); // if uname/pass correct it returns must be 1 row
if( $count == 1 && $row['userPass']==$password ) {
$_SESSION['user'] = $row['userId'];
$_SESSION['location'] = $row['userLocation'];
header("Location: home.php");
} else {
$errMSG = "Incorrect Login";
}
Home.php
$query = "SELECT * FROM machines WHERE locatie=".$_SESSION['location'];
$result = mysql_query($query);
Why Does this not work??
I can't figure out why the $_SESSION['locatie'] part does not work??
I thought it has the same value as in the other file.
There is a syntax error in your mysql query.
$query = "SELECT * FROM machines WHERE locatie='".$_SESSION['location']."'";
You want the $_SESSION['location'] value to be carried over to the home.php file, after redirection?
Make sure that $row['location'] has a value, and that session_start() has been invoked on both pages.
Try Checking by debugging the code like this
print_r($row);
If Yes then again do this:
echo $row['userLocation'];
Are you getting this value?
Then If You want to put value into session and den redirect make sure you initialize the session first by writing session_start(); and den add the value like
$_SESSION['location]= $row['userLocation];
But make sure check are you getting value before setting session.?
Better would be to solve if you update your code with print_r($row);
And please stop using sql functions as they are deprecated instead use mysqli and pdo .

Access Session data in codeigniter

$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'];

Session does not work between pages

I have this problem. Session does not work when I use with $_POST. If I add 63 manualy it will works across pages. I can see the output 63.
$_SESSION['name'] = 63;
echo $_SESSION['name'] ;
but this below won't work when I switch between pages. The $row['id'] output is also 63.
$cari = "SELECT id FROM dns_soa WHERE `origin` = '".$_POST['origin']."'";
$keputusan = mysql_query($cari);
$row = mysql_fetch_array($keputusan);
$_SESSION['name'] = $row['id'];
echo $_SESSION['name'] ;
When I go to 2nd pages I can see the output but when I went back to 1st pages the output is gone. Any idea?
Because when you are coming back it will again set the session variable, as this time you don't have the $_POST[origin] variable that leads to $row['id']="" and the session variable also NULL...
What you should do is..
$cari = "SELECT id FROM dns_soa WHERE `origin` = '".$_POST['origin']."'";
$keputusan = mysql_query($cari);
$row = mysql_fetch_array($keputusan);
if(!isset($_SESSION['name']))
$_SESSION['name'] = $row['id'];
echo $_SESSION['name'] ;
When you go back to the page where you have used $_POST, $_POST becomes empty, unless your browser posts it again. So the SQL query returns no result. Hence, $_SESSION['name'] becomes empty.
Also make sure you have invoked session_start on every page before using $_SESSION

Trouble with session variables

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

Categories