function session_input()
{
$session_value = $this->input->post('welcome');
$this->session->set_userdata('name', $session_value);
echo "<a href='". base_url()."/index.php/contact/session_output'> go to </a>";
echo "your session has been save " ;
}
Is this correct?
Retrieving Session Data
Any piece of information from the session array is available using the following function:
$this->session->userdata('item');
Where item is the array index corresponding to the item you wish to fetch. For example, to fetch the session ID you will do this:
$session_id = $this->session->userdata('session_id');
Note: The function returns FALSE (boolean) if the item you are trying to access does not exist.
Adding Custom Session Data
$this->session->set_userdata($array);
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
yes correct
$session_value = $this->input->post('welcome');
if($this->session->set_userdata('name', $session_value)){
echo "your session has been save";
}
you can read more about it here ->
https://ellislab.com/codeigniter/user-guide/libraries/sessions.html
if its single value session:
$this->session->set_userdata('session_name', 'session_value');
if its multiple value session:
$array_name = array('value1','value2','value3');
$this->session->set_userdata('session_name',$array_name);
array along with key
$array_name = array('key_name1'=>'value1','key_name2'=>'value2','key_name3'=>'value3');
$this->session->set_userdata('session_name',$array_name);
to get the session value
$var = $this->session->userdata('session_name');
Related
I have coded a internal messenger system for the company I work for and I am trying to make things easier for myself by making a admin panel for this so it can control the usernames, passwords and groups.
Basically I am trying to select a user from the array using a url ?user=username which will then return the user information and allow it to be changed. The problem I am having is to get the url to select the username in the array.
The array is basic:
$users = array(
'0' => array(
'username' => 'test',
'password' => 'test123',
'group' => 'office',
),
Like this?:
<?php
// get username from URL (GET METHOD)
$username=$_GET["username"];
// $users has all the info
foreach ($users as $u) {
// find the target user
if ($u["username"] == $username) {
// do something with $u
var_dump($u);
}
}
?>
Did you tried to do this:
$user_name = $users[0]["username"];
Try this (not tested)
$u = null;
foreach($user as $user) {
if(isset($_GET['user']) && $user['username'] == $_GET['user']) {
$u = $user;
}
}
$u variable should contains yours user record.
Since it's an array i assume you have multiple indexes. I guess a simple 'find' function might work.
function findUser($searchData, $users, $searchField) {
$retObj = array();
foreach ($users as $user) {
if (isset($user[$searchField]) && $user[$searchField] == $searchData) {
$retObj['username'] = $user['username'];
$retObj['password'] = $user['password'];
$retObj['group'] = $user['group'];
break;
}
}
return $retObj;
}
EDIT: Added searchField so u can do same for password and group. Searchdata is the value u are searching
I'm using a search system, in PHP, that adds the text searched to an array and put it inside a cookie using json_encode().
The problem is: I need to check if the cookie already exists and, if not, create it.
I'm using the following code to simply verify if it exists but without success:
{
$search = $this->input->post('search_text');
$types = $this->input->post('search_type');
$checkboxes = "";
if(!empty($types))
{
foreach($types as $v)
$checkboxes = $v.",";
}
//cookie
$this->load->helper('cookie');
//cookie's array
$search_history = array();
array_push($search_history, $search);
//cookie check 1
if(get_cookie('search')!=''){
echo "cookie exists";
}else
echo "cookie doesn't exist";
// set cookie
$cookie = array(
'name' => 'search',
'value' => json_encode($search_history),
'expire' => time()+86500
);
set_cookie($cookie);
//cookie check 2
if(get_cookie('search')!=''){
echo "cookie exists";
}else
echo "cookie doesn't exist";
//echo get_cookie('search');
//redirect('search/'.urlencode($search).'/'.urlencode($checkboxes));
}
I can create the cookie and get it's value, but I can't seem to find a way to check if it's already created in PHP code.
Have already tried with:
if(get_cookie('search')!='')
,
if(get_cookie('search')!=null)
and with
if(get_cookie('search'))
but neither of those seem to work.
EDIT:
As suggested, I'm now using this and it doesn't create the cookie.
//cookie
$this->load->helper('cookie');
//cookie's array
$search_history = array();
array_push($search_history, $search);
if(cookie('search') == false){
// set cookie
$cookie = array(
'name' => 'search',
'value' => json_encode($search_history),
'expire' => time()+86500
);
set_cookie($cookie);
}
Final EDIT
Problem solved.
//checks if the cookie exists
if($this->input->cookie('cookiename')!=''){
//exists
}
use get_cookie()
if (is_null(get_cookie('cookiename'))) {
//set yours cookie here
}
I am using Codeigniter to create an Autocomplete for user names:
Using the parameter : search-user?term=s I get the following data back as Json :
{"id":"1","value":"Stuart Blackett"}{"id":"2","value":"Simon Wilton"}
However when I am running the auto select, On the search for say "St" it brings back by name in the options, But also the ID as well.
How do I prevent this from happening? I suspect my loop could be the issue.
My PHP Function is as follows :
function search_user()
{
$term = $this->input->get('term');
$user_search = $this->vendor_model->search_user($term);
$user['response'] = 'false';
if(count($user_search) > 0)
{
$user['response'] = 'true';
$user = array();
foreach($user_search as $user)
{
$user = array(
'id' => $user->user_id,
'value' => $user->user_firstname .' '. $user->user_surname
);
echo json_encode($user);
}
}
}
{"id":"1","value":"Stuart Blackett"}{"id":"2","value":"Simon Wilton"} isn't valid JSON.
Try not echoing each $user's information separately - instead, build a new array of users and json_encode() that array. Example:
foreach($user_search as $user) {
$users[] = array(
'id' => $user->user_id,
'value' => $user->user_firstname .' '. $user->user_surname
);
}
echo json_encode($users);
im trying to write a code where a form will store all of its values inside session variables when the submit button is pressed and go to the next page where there is another form waiting to be filled up. when the submit button for the form in page 2 is pressed the variables will be stored in a session then the contents of the session variables/array will be inserted in the database. and i seem to be having problems
Summary:
Page 1 - User fills up form and the variables will be stored in a session array upon submit
Page 2 - user fills up another form and the variables will be stored in a session array upon submit
the session variables in page 1 and 2 will be inserted in the database
here is my code:
controller(site.php)
public function m1()
{
if(isset($_POST['m1']))
{
$suffix = $this->input->post("suffix");
$fn = $this->input->post("fn");
$mn = $this->input->post("mn");
$ln = $this->input->post("ln");
$nickname = $this->input->post("nickname");
$sex = $this->input->post("sex");
$bday = $this->input->post("bday");
$street = $this->input->post("street");
$city = $this->input->post("city");
$province = $this->input->post("province");
$region = $this->input->post("region");
$landline = $this->input->post("landline");
$cellphone = $this->input->post("cellphone");
$email = $this->input->post("email");
$edu_level = $this->input->post("edu_level");
$course = $this->input->post("course");
$school_name = $this->input->post("school_name");
$school_address = $this->input->post("school_address");
$school_province = $this->input->post("school_province");
$school_city = $this->input->post("school_city");
$school_region = $this->input->post("school_region");
$newdata = array('suffix'=>$suffix,
'fn'=>$fn,
'mn'=>$mn,
'ln'=>$ln,
'nickname'=>$nickname,
'sex'=>$sex,
'bday'=>$bday,
'street'=>$street,
'city'=>$city,
'province'=>$province,
'region'=>$region,
'landline'=>$landline,
'cellphone'=>$cellphone,
'email'=>$email,
'edu_level'=>$edu_level,
'course'=>$course,
'school_name'=>$school_name,
'school_address'=>$school_address,
'school_province'=>$school_province,
'school_city'=>$school_city,
'school_region'=>$school_region,
);
$this->session->set_userdata($newdata);
redirect(base_url() . "site/cmain/m/2");
}
}
public function m2()
{
if(isset($_POST['m2']))
{
$nature_complaint = $this->input->post("nature_complaint");
$newdata = array('nature_complaint'=>$nature_complaint);
$this->session->set_userdata($newdata);
$this->db->insert('myself', $newdata);
redirect(base_url() . "site/cmain/m/3");
}
}
the problem with my code is that the session variables for page 2(public function m2()) is the only one that gets inserted in the database.
is there a way to fix this?
thanks
In the first function (M1)
set your newdata in this way
$this->session->set_userdata('newdata', $newdata);
In second function (M2)
public function m2()
{
if(isset($_POST['m2']))
{
$nature_complaint = $this->input->post("nature_complaint");
$newdata = array();
$newdata = $this->session->userdata('newdata');
$newdata['nature_complaint'] = $nature_complaint;
$this->session->set_userdata('newdata', $newdata);
$this->db->insert('myself', $newdata);
redirect(base_url() . "site/cmain/m/3");
}
}
Your $newData variable is getting reset in your second function. you have already stored $newData array to session. after resetting values in second function session contains only data from second function. you need to add data in $newData instead resetting it.
Site built on Code Igniter framework. I have two links (like and dislike). When you click the link, the correspond value increases or decreases. You can see the site: http://joke.guluzade.com/jokes/view/24
What I want: That user can change this value only once. I do it by cookies. For example, if user click to "like" link the value +5 became +6. Then if you click "dislike" the value must not change. User should have only one chance to like or dislike. Now it doesn't work correctly, if you click like you are able to click dislike too, but I want only change the lie or dislike value only one time.
How I do: I check if the cookie is set, function does nothing, if not it sets cookie and changes value. But if cookie set for like, when you click dislike it doesn't see that cookie.
Here is code:
function vote ($mm, $id){ //get the parameters (like or dislike and id)
$name = $mm;
$value = (int)$id;
$time = time()+3600;
if(isset($_COOKIE[$value])){
redirect($_SERVER['HTTP_REFERER']);
} else {
SetCookie($value, $name, $time);
if($name == "like"){
$this->db->select('like');
$this->db->where('id', $id);
$query = $this->db->get('jokes');
$data = $query->row_array();
$likes = $data['like'];
$likes++;
$dd = array();
$dd['like'] = $likes;
$this->db->where('id', $id);
$this->db->update('jokes', $dd);
redirect($_SERVER['HTTP_REFERER']);
} else {
$this->db->select('dislike');
$this->db->where('id', $id);
$query = $this->db->get('jokes');
$data = $query->row_array();
$likes = $data['dislike'];
$likes--;
$dd = array();
$dd['dislike'] = $likes;
$this->db->where('id', $id);
$this->db->update('jokes', $dd);
redirect($_SERVER['HTTP_REFERER']);
}
}
}
Can anybody say, what I do wrong?
use get_cookie('some_cookie') OR get_cookie('some_cookie',TRUE); instead of $cookie[$value].
moreover set cookie for full domain using
$cookie = array(
'name' => 'The Cookie Name',
'value' => 'The Value',
'expire' => '86500',
'domain' => '.some-domain.com',
'path' => '/',
);
set_cookie($cookie);
You can find the answer from following posts.
How can I set a cookie and then redirect in PHP?
http://www.tek-tips.com/viewthread.cfm?qid=1509045