CodeIgniter Session syntax - php

Can someone quickly help me out with CodeIgniter's syntax. I need to access an array I stored in the session's userdata and I cant figure out the proper syntax.
<?php echo $this->session->userdata['user_session']['first_name']; ?>
gives me this error:
Fatal error: Cannot use object of type stdClass as array
All of the answers given in this Question dont work:
Access array variable in session (CodeIgniter)

This is how you get session data:
echo $this->session->userdata('first_name');

Been a while since I've worked in Codeigniter, but if I can remember correctly, when you store an array like you've stated, you'd call it like this:
$this->session->userdata("user_session")['first_name'];
Let me know if that works?
Or you can store that data to a variable, and call the array that way. Like such:
$data = array("bar" => "the_value");
$this->session->set_userdata("foo", $data);
$foo = $this->session->userdata("foo");
echo $foo["bar"]; //Outputs the_value
Let me know if that helped.
However, just to let you know.. Normally, storing the session data goes as follows:
$this->session->set_userdata("first_name", "value");
Really no need to go and set your own array inside of userdata, because that's generally what the userdata array is for.

I found the proper syntax. Well, at least one way to go about it. #Matt GrubB was the closest and put me on the right track.
$temp_session = $this->session->userdata('user_session');
echo $temp_session->first_name;
Since userdata is an object full of info created when I query my database, the easiest way to access the data is to take it and put it in another temporary array. You then have to stab it. I kept stumbling by trying to do $this->temp_session->first_name or something of the like.

Related

How can I write php codes as string?

I have this code:
<?php $myarray->appends(['action' => $_GET['desc']])->render() ?>
When there is action argument in the url, then my code works as well. Otherwise it throws this error message:
Undefined index: action (View:
/var/www/html/myweb/resources/views/mypage.blade.php)
So I need to do this ->appends(['action' => $_GET['desc']]) dynamically. Something like this:
<?php
if ( isset($_GET['desc']) ) {
$append_param = "->appends(['action' => $_GET['desc']])";
} else {
$append_param = "";
}
$myarray.$append_param->render();
?>
But I'm pretty much sure my code won't work .. I wore code about just for showing you what was my point.
Anyway, can anybody tell me how can I do that in a right way?
All I'm trying to do is: appending action argument to the pagination-links if it already exists. Otherwise I don't want to append it.
You should never take input from a user-controllable source and execute it. Doing this is a major security risk, and it will probably open more security holes in your server than you'd be able to patch in a lifetime.
If you really, really must do this, you can use eval(). But please, make sure you understand the security implications of this.
i think you are just horribly overthinking the whole situation. you don't have to put your code in a string to execute it conditionally, you can just write it as code itself.
having no further information about how your class works internally, the most straightforward and basic way would be:
if ( isset($_GET['desc']) ) {
$myarray->appends(['action' => $_GET['desc']])->render()
} else {
$myarray->render();
}
You cannot do that.
First, if you want to check for a valid key in an array, use "array_key_exists(key, array)" which is hugely recommanded.
if ( array_key_exists('dec',$_GET) ) {
Then, if I understand, you want to "append" data to an Array ? That means adding a value to a key in this array.
This can be done easily with PHP :
$array [ $key ] = $value;
Be very careful on your variable names :
$myarray.$append_param->render();
This makes no sense, if it's an array, you only should use [] on it (or array functions), but if it's an object, you only can use -> on it.

Selecting only Non-file Parameters from Laravel Request

For one of my Laravel Web app I want to log all the Request Parameters(Post as well as Get) in database in Json Format for that I am using $request->all() Method, which results in an exception when user tries to upload any file.
that's why I want a way to select only Serializable Parameters from the request.(for get as well as for post Requests) or a way to select all the request parameters except files.
Request::except([]) will not work for me since in Except method we will have to provide the file parameter names.
In my project, i used this except for many fields like below,
$input = $request->except('first_name', 'middle_name', 'last_name', 'address',...);
It is work fine for me.
I stored all the remain values into $input and store values from that input variable.
Please try this one.
In your case please take this debug code for test once, might be you like it to use in your current work
$allRequestParams = array_map(function($input) {
return !is_array($input) ? $input : false;
}, $request->all());
echo '<pre>';
print_r($allRequestParams);
echo '<pre/>';
die;
Since any of the answer didn't work for me I did lots of reading and some digging about laravel but still I could not find the specific solutions I was looking for, so I did a small hack, instead of using Laravel's Request Object and pulling parameters from there I simply used PHP's built in $_REQUEST parameter.
Eg.
$non_file_parameters = $_REQUEST;
$_REQUEST will have both Get as well as Post Parameters except file Parameters coz in Core PHP for files we have $_FILES super global variable.
Thanks guys for your efforts...

Unserialized COOKIE returns empty

I'm trying to save an array into a cookie, in a serialized manner. So what I do is this:
$serial_auth = serialize($_SESSION['auth']);
setcookie("auth_cookie", $serial_auth , 2592000 + time());
and in the next page I'm trying to use this data like this:
if(isset($_COOKIE['auth_cookie']))
{
$_SESSION['auth'] = unserialize($_COOKIE['auth_cookie']); //but it returns an empty array.
}
now the strange thing is the whole thing works in my localhost, but it does not work on 000webhost site.
and a note: when I try to echo those, I get this:
$_SESSION['auth'] =
Array ( [status] => ok [userid] => 1 [username] => user11 [visiblename] => user11 )
SERIALIZED =
a:4:{s:6:"status";s:2:"ok";s:6:"userid";s:1:"1";s:8:"username";s:6:"user11";s:11:"visiblename";s:6:"user11";}
This may be a PHP configuration issue, but I would like to learn if there is a way for this, without changing any PHP configuration. Thanks for any help.
This has severe security drawbacks and shouldn't be done.
An attacker can set the cookie value to anything, including serialized objects. These objects may execute code when recreated. So an attacker may execute code on your machine by sending you a properly crafted serialization string! One cannot want attackers to be able to do this.
See the warning at the bottom of this page: http://www.php.net/manual/en/function.unserialize.php
The second thing is: Why the hell do you need the authentication info in a separate cookie, and what is connected with it? What would an attacker be able to do if he changes any of the values, especially the userid or the status? I assume he might gain access to things he shouldn't be able to.

accessing array from another page in php

I would like to know how can I access or read the array from another page. I am working on a PHP page which contains the array, and I want to display the content of that array on another PHP page.
For example, I used the following method in a PHP file and I want to get the content of the array in another PHP file. what is the method that is going to receive the array's content in the second page.
<?php
$r = new HttpRequest('http://localhost/sameh.php', HttpRequest::METH_POST);
$r->addPostFields(array("n" => 'heba')) ;
$r->send();
?>
This code is in the first page but I don't know what to write to receive it on the second one.
Maybe my question was not that clear and sorry about that ,, I want to find a way to access the array that is defined inside the HttpRequest() class on another page. So that the array "n" that include value "heba" will be displayed on another.php page. this is what make me thinking that the problem is on how to access the content of the array on the second page.
I tried the session and It sends the array to another page ,, but when I tested with the
httpRequest() method it doesn't send the content of the array "heba" to the second page.
Thanks for your help.
Sounds like this is a job for Sessions.
You can read the complete session guide here
In the script that has the array you can do something like:
session_start();
$_SESSION['array'] = $array;
In the next script you access it similarly:
session_start();
print_r($_SESSION['array']);
Include that file in your php file where you want to use that array. This should solve your issue PHP - How to send an array to another page?
I'm not sure what the HttpRequest class is, but at a guess, it's POSTing variables to the sameh.php file. You should be able to access the variable on the next page by doing this:
echo $_POST['n'];
Which should print "heba".
Note that to work with serialized arrays, you need to use POST as the form's transmission method, as GET has a size limit somewhere around 1024 characters.
I'd use SESSIONs wherever possible.
You can use serialize() and unserialize() on your array to represent it as a string and pass it via POST.

Formatting the POST array to an SQL insert\update string in codeigniter 2

I'm trying to automate form creation and submission in codeigniter.
Basically what I want is to find a way to go over all the data in the POST array and format it correctly to an insert or update sql query.
The problem is I don't know how to access to whole POST array in CI, all I know of is the $this->input->post(field_name) way which only gives you a specific field.
Ideally I would want to send the POST array to the $this->db->insert_string() or $this->db->update_string() to do the job for me.
I know I can still use the php native $_POST array, but this is not recommended and not as secure as CI's input class.
Anyone know a way to do this?
Thanks,
Amos
Eventually I found out that the input class cleans the $_POST array automatically (not talking about XSS cleaning) and so the only advantage to use $this->input->post(something) is that it checks if that key exists.
Since I need the whole array I don't need that check and can safely use $this->db->insert_string($_POST).
If I you do want XSS cleaning you can either turn it on globally in the config or use geocine's answer (I would go for a mix of the 2 examples he gave).
Another way to go if you want the whole array with XSS cleaning and without it turned on globally is to go with WanWizard's Input library extension found here: http://codeigniter.com/forums/viewthread/172705/#821150
foreach($_POST as $key => $value) {
$value = $this->input->post($key);
//do something
}
or
$keys = array_keys($_POST);
for($i=0,$max=count($keys);$i<=$max;$i++)
{
$value = $this->input->xss_clean($_POST[$keys[$i]]);
//do something
}

Categories