Fetch From a Dump Array - codeigniter

I have a dump array and want the value of email, how can I do it?
string(614) "a:6:{s:10:"session_id";s:32:"f0fd7825ad5e0760c635f136e1f508db";s:10:"ip_address";s:15:"202.142.176.142";s:10:"user_agent";s:120:"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36 OPR/27.0.1";s:13:"last_activity";i:1423743791;s:9:"user_data";s:0:"";s:9:"logged_in";a:8:{s:7:"user_id";s:2:"78";s:8:"username";s:5:"Ahsan";s:7:"role_id";s:1:"1";s:9:"user_type";s:8:"Reseller";s:19:"reseller_company_id";s:1:"1";s:9:"full_name";s:14:"Muhammad Ahsan";s:5:"email";s:17:"ahsan#tasmimy.com";s:5:"thumb";s:18:"14236608537282.jpg";}}e92d7c14027cd35d0a2e45e3e0d50328"

All you need to do to get the session is after you have set your sessions.
Auto load session library and add encryption key in config.php and enable sessions. Codeigniter 2.2.1 and Codeigniter 3 are different slightly.
$data = array(
'isLogged' => true,
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
);
$this->session->set_userdata($data);
Then you should be able to use echo $this->session->userdata('email');

Related

How can I simulate a broswer request in using Guzzle

I am trying to get cookies with from a site, but the cookies I receive are not valid while vs. when using the cookies received in the browser are valid. Here is the header values:
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
$cookieClient = new Client(
[
'base_uri' => $url,
'headers' => [
'User-Agent'=>'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
'Connection'=>'keep-alive',
'Content-Type'=>'application/json',
],
'connect_timeout' => 20,
"timeout" => 150
]);
$jar = new \GuzzleHttp\Cookie\CookieJar;
$r = $cookieClient->request('GET', 'https://example.com', [
'cookies' => $jar
]);
$arrayJar = $jar->toArray();
$cookiesValue = $arrayJar[2]["Value"];
log::info($cookiesValue);
If Guzzle (V7) is not able to to do so, is there another mehtod to get cookies as while mimicing a browser? The cookies are meant to be utilized in the project.

Laravel Guzzle HTTP keep returning 404

I'm using openlibrary's API to fetch book data. I have problem with some API (not all) which keep return 404, but when i'm using postman to test, it's working fine.
$response = Http::get('http://openlibrary.org/search.json', [
'q' => 'Johngreen'
]);
dd($response->body());
I tested this endpoint with Laravel and I had the same problem but it works on Postman and browser.
The problem is related to the User-Agent of the client. If you don't set it, Openlibrary cannot check the source of the API call (browsers and Postman send the own user agent).
I solved with this code:
$guzzle_client = new \GuzzleHttp\Client();
$response = $this->guzzleClient->get("http://openlibrary.org/search.json?q=Johngreen", [
'headers' => ['User-Agent' => 'PUT AN USER AGENT HERE']
]);
$response_body = json_decode($response->getBody()->getContents(), true);
dd($response_body);
Try to use this User Agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36
or use another one
openlibrary is not accepting guzzlehttp user agent, try using only curl or a browser user agent.
Both of these works
using curl user agent
try{
$response = Http::withHeaders([
'User-Agent' => 'curl/7.65.3'
])->get('https://openlibrary.org/search.json?q=Johngreen');
dd($response->body());
} catch(\Illuminate\Http\Client\RequestException $e){
// Log your errors
}
using browser user agent
try{
$response = Http::withHeaders([
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
])->get('https://openlibrary.org/search.json?q=Johngreen');
dd($response->body());
} catch(\Illuminate\Http\Client\RequestException $e){
// Log your errors
}
For more info on default user agent used by guzzle, see docs
Try code below first to view response all content.
Http::dd()->get('http://openlibrary.org/search.json', [
'q' => 'Johngreen'
]);
You are using Guzzle. Try using the below code for consuming API.
$client = new \GuzzleHttp\Client();
$response = $client->get('http://openlibrary.org/search.json', [
'query' => ['q' => 'Johngreen']
]);
dd(json_decode($response->getBody()));

Pagination giving odd error

I'm trying to implement pagination and here is what I have so far.
In my main controller:
public function showIndex()
{
$countries = Country::paginate(5);
return View::make('index')->with('countries', $countries);
}
And in my view I'm simply doing:
<?php print_r($countries); ?>
However, that outputs an insanely long error, too long to post here in full:
Illuminate\Pagination\Paginator Object ( [factory:protected] =>
Illuminate\Pagination\Factory Object ( [request:protected] =>
Illuminate\Http\Request Object ( [json:protected] => [sessionStore:protected]
=> [attributes] => Symfony\Component\HttpFoundation\ParameterBag Object (
[parameters:protected] => Array ( ) ) [request] =>
Symfony\Component\HttpFoundation\ParameterBag Object ( [parameters:protected]
=> Array ( ) ) [query] => Symfony\Component\HttpFoundation\ParameterBag
Object ( [parameters:protected] => Array ( ) ) [server] =>
Symfony\Component\HttpFoundation\ServerBag Object ( [parameters:protected] =>
Array ([HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0)
Gecko/20100101 Firefox/36.0 [HTTP_ACCEPT] =>
text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
[HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.5 [HTTP_ACCEPT_ENCODING] => gzip,
deflate [HTTP_COOKIE] =>
What am I doing wrong? Am I missing something?
I'm going to post an answer here for the sake of clarification. The "error" you're getting isn't an error at all. It is, however, insanely long because it's printing recursively the entire collection returned from the ::paginate(5) method. If you scroll to the very bottom of this print_r statement, you should be able to make our your result set. For example, look for the id of one of your results and you should see something like Array ([id] => 1 [column_2] => x [column_3] => y ... where column_N are the names of the columns in your table.
Easiest rule of thumb when using Laravel:
If you return a collection, use a foreach loop.
And by that I mean if you use the methods ->get() or ->paginate(), go through your results using #foreach($items AS $item).
Hope that provided some clarification.
To answer my own question, all I had to do was add a loop in the view to loop through the database results:
#foreach ($countries as $country)
<div class="title">{{ $country->name }}</div>
#endforeach
{{ $countries->links() }}

can not store data in codeigniter session

I am trying to store data in my session variable, but i am doing something wrong obviosly.
I have the following code:
$formValues = $this->input->post(NULL, TRUE);
echo "Form<pre>";
print_r($formValues);
echo "</pre>";
$this->session->set_userdata($formValues);
$sessionFormValues = $this->session->userdata('formValues');
echo "MySess<pre>";
print_r($sessionFormValues);
echo "</pre>";
echo "Sess<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
The first print_r returns:
Array
(
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
)
The second print_r returns just MySess (empty array)
while the third print_r returns the following:
Array
(
[session_id] => 639ca0b53c8e68cda4e6d61605c4d227
[ip_address] => 94.68.157.85
[user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
[last_activity] => 1393239192
[user_data] =>
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
)
Anyone can point me what I am doing variable $sessionFormValues returns an empty array? How can i get only the formvalues data in a separate variable?
Regards, John
Your data is getting saved in the session fine but the problem is that the 'formValues' key doesn't exist. You need to change
$this->session->set_userdata($formValues);
to
$this->session->set_userdata('formValues', $formValues);

CodeIgniter Session Issues

I set session in a controller function like
$search = array(
'search_count' => count($data['result']),
'projectInfo' => $data['result']
);
$this->session->set_userdata($search);
where $data['result'] is an array;
but if I try to access this variable in other function of same controller it shows nothing:
print_r($this->session->userdata('projectInfo'));
though on using print_r($this->session->userdata('search_count')); it shows correct value.
also if I use print_r($this->session->all_userdata()); in second function of same controller it does not show array value index which I have already set in first function
Array
(
[session_id] => 4adf3a42ee64ffca2b2f273cb293a10a
[ip_address] => 127.0.0.1
[user_agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1
[last_activity] => 1347689522
[user_data] =>
)
If I'm correct you can't save arrays into a session without serializing them first.
$search = array(
'search_count' => count($data['result']),
'projectInfo' => $data['result']
);
$this->session->set_userdata($search);
Becomes:
$search = array(
'search_count' => count($data['result']),
'projectInfo' => serialize($data['result'])
);
$this->session->set_userdata($search);
Now if you want to retrieve the array:
$data = unserialize($this->session->userdata('projectInfo'));
print_r($data);
Please note that you should use the database to store sessions when you are setting large amounts of data in a session.
config.php
$config['sess_use_database'] = TRUE;
Thanks I added Native PHP Session Class

Resources