php array to call wordpress get_current_user_info - php

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

Related

Get and store a URL segment into a cookie with CodeIgniter

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 :-)

Simultaneously insert data into same table with PHP (laravel)

I'm not sure if this is even possible but what the heck :) I have two URL, both trying to insert different data into the same table. Example
We have table "food" and two URL with functionality that insert into FOOD table some values
http://example.com/insert_food_1
http://example.com/insert_food_2
When loading both URLs in the same time, every one of them waits for the other one to finish first and afterwards inserts into the DB the specific values.
I know this is called multithreading or something... but i'm not sure if this can be done with PHP (or Laravel).
Any help would be much appreciated. My code looks like so ...
$postToInsert = Post::create(
array(
'service_id' => 1,
'custom_id' => $post->body->items[0]->id,
'url' => $post->body->items[0]->share_url,
'title' => $post->body->items[0]->title,
'title_tags' => $tags,
'media_type' => $mediaType,
'image_url' => $imageURL,
'video_url' => $videoURL
));
$postToInsert->save();
I kind of fix it. Opening them in separate browsers or php them via CURL from terminal solves the problem.
Thanks for all the help

CakePHP / PHP If Statement User Login Error / Issue - Both conditions are set true?

I am using CakePHP 2.2.2 and building session information with it, to store all the users login details, so that I can access them though out my whole site. The code I have written is below:
if (($GetPass == $this->data['Menu']['password'] && $GetUsers == $this->data['Menu']['username'])) {
$this->Session->write('LoginData', array(
'FristName' => $SQL[$key]['User']['fristname'],
'SurName' => $SQL[$key]['User']['surname'],
'UserName' => $SQL[$key]['User']['username'],
'AccessLevel' => $SQL[$key]['Role']['title'],
'Email' => $SQL[$key]['User']['email']
));
} else {
$this->Session->setFlash('Sorry Login Details Do Not Match Our Records', array('element' => 'failure'));
}
The issue I have with this is driving me up the wall, both conditions are always true. So even if the username & password are within the database, it is still setting the flash message? I just can not see why? The 'else' part of a if statement should only run if the conditions set within the if statement are false, right? So why is my flash message always being set?
There might be two reasons, first the password field you get from CakePHP Model might be encrypted. Use debug($GetPass); and check if it is encrypted or not. Second you are trying to use $this->data in controller which should be $this->request->data. Standard way to do what you are trying to do is given here: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

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.

JSON code for my radio station website?

This is part of my radio station website's PHP coding:
http://pastebin.com/D1TUMNSc
I've got the PHP to work; but getting the JSON to work is a problem, I'm not sure how to do it with the variables I've got...
Currently the schedule page shows as blank, since I'm not sure what to do.
The Javascript is here: http://pastebin.com/P1ydnVCK
Basically, I'm having trouble trying to create a JSON to fit within the variables of the Javascript, it's new to me, this area of programming...
What would you suggest?
I've had a look around the net at JSON resources but am not quite sure what to do for this one!
Just a quick look so this may not be everything you need, but.... You need to do something like this....
$json->schedule = array();
// Loop Here
$show = array(
'longname' => 'xxxxx',
'description' => 'xxxxxx',
'weblink' => 'http://xxxx.com',
'showimage' => 'xxxx',
'startminutes' => '0000000',
'endminutes' => '000000',
);
// End Loop
$json->schedule[] = $show;
echo json_encode($json);
In your javascript anything prefixed with _s needs to be a variable... Ex: this._s.longname

Categories