Get and store a URL segment into a cookie with CodeIgniter - php

For a kind of conversion tracking, I want to store a part of the URL into a cookie and use this into my model functions.
So I think about https//:www.example.com?track=this
How I can get the part of the track (=this) with CodeIgniter and save it into a cookie?
To create a cookie I think that's the way,
$this->load->helper('cookie');
$cookie = array(
'name' => 'track',
'value' => '???',
'expire' => '300',
'secure' => TRUE
);
set_cookie($cookie);
How I can get the part of URL and set cookie etc with CodeIgniter on every possible page? So I mean, no matter over which page the user call the website, I need to make sure that this tracking info is stored in every case.
Go get the cookie later, I think I can use $cookie= get_cookie('track');
Thanks for showing me the way to do it with CodeIgniter.

Mainly you need to use parse_url to extract url to query string and then parse_str to extract query string.
First, you need to get query string from you URL
$query = parse_url('https//:www.example.com?track=this&h=1');
echo'<pre>';print_r($query);
Output:
Array
(
[path] => https//:www.example.com
[query] => track=this&h=1
)
Then you need to extract query
parse_str($query['query'],$array);
echo'<pre>';print_r($array);die;
Output:
Array
(
[track] => this
[h] => 1
)
From here you can store your above array information in cookies

to get the wanted part of the query-string there is a built-in function of Codeigniter
$myval=$this->input->get('track', TRUE);
I've add this with a part to store and get the cookie in the header of my mainlayout-theme.
So it will works :-)

Related

update value in config.php with another php script

I include a config.php file in all of my pages.
I also have a cron job which updates a total count of a table, into another table each hour.
I want to also write that value into the config and save if possible.
my config is in the structure:
return array(
'stats' => array(
'total_count' => 130000,
'another_stat' => 100000,
),
'other_config_value' => 'value',
)
and I include it in my pages, at the top like:
$config = include('../php/config.php');
$total_count = $config['stats']['total_count']
just so I don't have to query the database on each page, every time it loads as I want to show that total on all pages.
You can keep your config in JSON format and use functions json_decode() to read it. If yout want update it you can use json_decode() to make an array from JSON string, update this value in array and use json_encode() to make JSON string from array. After that you can save it to file with file_put_contents().
$config = json_decode(file_get_contents('config.json'));
$config['value'] = $newValue;
file_put_contents('config.json', json_encode($config));

php array to call wordpress get_current_user_info

I've been reading and tweaking code for three days and can't figure this out. Because I'm new to php. The developer that wrote this php login script didn't code it for WordPress and not for an automatic login like I need. I think I've figured out the automatic login at the end of the code I didn't post here.
He created this code for his array that will have set values for the three api strings that never change, but I need to include the logged in WordPress user's username and password to be included in his array. The array info is passed by xml later in the script. This is for an automated SSL login to another server and when an image is clicked it runs the php code.
This is the code I just got from him, but I don't believe it's correct because all of the colors just changed in Notepad++ when I inserted it:
// Set the Query POST parameters - array
$query_vals = array(
'api_username' => 'api-username-goes-here',
'api_password' => 'password-here',
'api_key' => 'api-key-here’,
'username' => $current_user[‘user_login’],
'password' => $current_user[‘user_pass’]
);
I know the last two lines can't be correct. What's the correct code to access the WordPress get user variables to use with his array?
Any help is greatly appreciated.
Change ‘ to '
// Set the Query POST parameters
$query_vals = array(
'api_username' => 'api-username-goes-here',
'api_password' => 'password-here',
'api_key' => 'api-key-here',
'username' => $current_user['user_login'],
'password' => $current_user['user_pass']
);
You need to call this somewhere in your code first:
global $current_user;
get_currentuserinfo();
Then you'll want to change your array to stuff like this:
'username' => $current_user->user_login

Add to A $_POST Array through Multiple Refreshes

I need to be able to store the $_POST data from a form (I'm doing that with the SESSION variable) and I need to add data to the $_POST array without overwriting the previous data.
Here's my original thread about the SESSION variables: Store Array Variables Through Page Refresh with PHP
Here's an example of what I need to do:
There's a form on a page that you can put the width and height of a wall to calculate out how much paint/labor would be needed to paint the walls. Once the first wall dimensions are entered, the user can click the submit button labeled "Add Wall" to add another wall to their order.
After the first wall input, the POST data would return something like this:
Array ( [wallW] => Array ( [0] => 9 ) [wallL] => Array ( [0] => 10 ) )
After the second wall is entered it would look something like this:
Array ( [wallW] => Array ( [0] => 9 [1] => 20 ) [wallL] => Array ( [0] => 10 [1] => 15 ) )
This would continue on until the user has entered all of their walls.
I've tried doing array_merge, array_append, etc, but none of those seem to be working; it keeps overwriting what's tin the POST data, even if I bring the POST data into another array.
I know the easy thing would be to throw some jQuery in there, but this is for a PHP class (and we're not allowed to use SQL yet) so I'd like to try to figure this out with just PHP.
Any help would be awesome.
Thanks,
Chelsea
I don't know why this has been downvoted, i find it rude as you are obviously new to php. But you just need to re-post your post vars in subsequent requests. So you would need to serialize you existing post objects as perhaps hidden fields. What you are missing is that php / http is stateless. So nothing is preserved between requests, another easy solution may be to move these POST variables to SESSION variables.
I see you've added the POST data to the session, but there is no reason to add it back to post, just keep appending to session and then use it from there. adding things to _POST wont force them to be posted next time if thats you are trying todo, see the part about serializing them to hidden fields.
Good luck, welcome to programming for the web.
The way you are constructing array seems little odd to me. Instead I would do something like.
if(!empty($_POST))
{
$wall = array('w' => $_POST['w'], 'h' => $_POST['h']);
$_SESSION['walls'][] = $wall;
}
Please give this a try.

Do query strings need to be manually sent to POST requests

This is a CakePHP / General PHP question.
In my application I use a query string like /login?continue=/admin/posts
This query string is used to redirect users to the URL in the query, but it doesn't work so it seems as though the app can't see the string...
This has got me wondering as basically when you arrive at the page with the string it's a GET request where as when you login, it becomes a POST or XML request (if using AJAX). Do I need to add the query string manually to the form for the POST to see it?
Either in the form action or a hidden input? Or am I barking up the wrong tree?
I'm currently grabbing the query like so:
if(isset($this->params['url']['continue']))
{
$pathtoredirect = $this->params['url']['continue'];
}
else
{
$pathtoredirect = $this->Auth->redirect();
}
But that's within the POST request so perhaps the query is lost... and adding it to a hidden input would not solve the problem with the current code so I would either change the code to look at the hidden field or pass the query with the action on the form?
e.g. <form action="/login?continue=/admin/posts" method="post">
Am I correct in thinking this? And would anyone be able to offer solutions or pros and cons of the two methods I mention?
In short I'm asking how to add the query string to my form action value
It currently looks like:
php echo $this->Form->create('User',
array(
'id' => 'loginform',
'type' => 'post',
'url' => array
(
'admin'=>false,
'controller' => 'users',
'action' => 'login'
)
)
);
So how would I add the query string to the form?
Thanks
When receiving a POST request you can receive both POST and GET variables through the superglobals $_POST and $_GET.
You can either send your paramater in $_GET by including it in the form's action attribute or send it in $_POST by creating an <input type="hidden"> tag within the form
A slash (/) is a reserved character. Encode it with %2F.
http://blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy
The solution was to do this:
<?php echo $this->Form->create('User',
array('id' => 'loginform', 'type' => 'post',
'url' => array('admin'=>false,'controller'=>'users','action'=>'login','url'=>$this->params['url']['continue']))); ?>
as I have a route already setup to handle the additional URL parameter:
Router::connect('/auth/login', array('controller'=>'users','action'=>'login'));
Router::connect('/auth/login?continue=:url',
array('controller'=>'users','action'=>'login'),
array(
'url' => '[A-Za-z0-9/\._-]+',
'pass' => array('url')
));
If anyone sees any issues with the way I do this though, please feel free to comment.

PHP how to pass URL parameters to create a new URL

Very newbie question: setting up a Meraki wireless EXCAP walled garden, and will have users land on a terms-of-service.php (simple checkbox)... upon submission, will land on page with other info THEN need to pass to open web. Need to grab first URL, save it, pass to page2.php, and then out to web.
Meraki's example of incoming URL (when user attempts to access wireless):
http://MyCompany.com/MerakiSplashPage/?base_grant_url=https://example.meraki.com/splash/grant&user_continue_url=http://www.google.com&node_id=222222&gateway_id=222222&client_ip=10.222.222.222
Then "When you are ready to grant access to the user, send the user to GET['base_grant_url'] + "?continue_url=" + GET['user_continue_url']. In the case of the example above, this URL would be:
https://example.meraki.com/splash/grant?continue_url=http://www.google.com
Going in circles on how to do this, any suggestions would be much appreciated.
Use rawurlencode to encode the value properly:
'http://MyCompany.com/MerakiSplashPage/?base_grant_url='.rawurlencode('https://example.meraki.com/splash/grant&user_continue_url=http://www.google.com').'&node_id=222222&gateway_id=222222&client_ip=10.222.222.222'
You can also use http_build_query to build the query automatically:
$query = array(
'base_grant_url' => 'https://example.meraki.com/splash/grant&user_continue_url=http://www.google.com',
'node_id' => '222222',
'gateway_id' => '222222',
'client_ip' => '10.222.222.222'
);
'http://MyCompany.com/MerakiSplashPage/?'.http_build_query($query)
Your final URL would be:
$_GET['base_grant_url']."?".$_GET['user_continue_url'];

Categories