CakePHP : call controllers functions in external php files - php

This is my situation:
1) a simple_php.php file in webroot folder
2) the users_controller.php in the app/controllers
3) the follow function in the users_controller.php :
function login_flat($user_id){
$user_data = $this->User->find('first', array(
'conditions' => array('User.id'=>$user_id)));
if($this->Auth->login($user_data)){
$this->Cookie->write('User.id', $this->Auth->user('id'), true, '+2 hour');
$this->Session->setFlash(__('Welcome back !', true));
$this->redirect(array('controller'=>'posts', 'action'=>'index'));
exit();
}
else{
echo 'ERROR!!';
exit();
}
}
(a simple function to login user. NOTE : I know it's not safe , but it's for test only)
Now, If I call via browser the function ( eg. www.mysite.com/users/login_flat/13 ) it WORKS ! but ... I need to call it via an other file, the simple_php.php located in the webroot of my domain.
I try with the php "header" function. It "call" (redirect) to the login_flat function , but the login not work !!! :(
Have any suggestion ? thx in advance.
P.S.
I'm using cakephp 1.2.6
------------- EDIT ------------------
I solve my trouble reading this:--> Solution

From the OP:
this is the code I uses for the redirect in the simple_php file :
header("Location:http://".$subdomain_locale.".".$site_subdomain.".com/users/login_via_app/".$user‌​_id."");
Which will redirect to
/users/login_via_app/13
Which is the wrong url, as it should be redirecting to
/users/login_flat/13
Also be sure to put 'exit();' after sending the header and check that nothing else is output before that, otherwise your redirect won't work
And, please check if the $sudomain_locale and $site_subdomain variables contains the right value, or debug the whole URL using this, instead of performing the redirect;
echo "this is the URL that will be redirected to:<br>";
print_r("Location:http://".$subdomain_locale.".".$site_subdomain.".com/users/login_via_app/".$user‌​_id."");
exit();

Related

Session redirect to home when id is null with codeigniter 3.0

Here is my code in controller:
$newdata = array(
'uid' => $usern,
'ou' => $filter
);
$this->session->set_userdata($newdata);
$this->masterpage->view('User/homeuser');
And here is my code in views:
<?php
$user = $this->session->all_userdata();
if ($this->session->userdata('uid')=='') {
redirect('main/main', 'refresh');
}
?>
However when I type link: http://localhost/[system_name]/index.php/[controller_name]/index
(link to show when uid is null, the page should redirect to main)
Only blank page is appearing. It seems it didn't redirect to the main/main page. Can anyone help me ? Thanks.
You can not do redirect in views. Use controllers for this, so move your logic to check session data from view to the controller.
View is rendered at the very end of your request so if there is some output, php will not redirect you to anywhere. You see blank page probably because you turned off errors display, but I am sure if you check php error log you'll see there is an error "can not modify headers" or so.
If you deadly want to do redirect in views, use Javascript for that.
This is the most silly things I've done. I just forget to import session from library. So should in my public function I put this function.
$this->load->library('session');
This is what happen when you try to built two system simultaneously. Anyway thanks for the response and tried to solve it for me.

Laravel 5 - session doesn't work when include a URL in PHP

I'm new in Laravel and PHP.
After I login successfully, the page redirects to index
Controller code as follow:
Session::save();
Redirect::to('index')
index.php code as follow:
include(URL('index/index_top'));
include(URL('index/top_banner'));
Here is the problem: session data is available in index.php, but unavailable in index_top.php. Page index.php display normal.
My English is poor, hope you can get my point.
Here, I just found that the sessionid was changed after include(), Why? How can I keep the sessionid.
Attach:
routes.php as follow:
Route::group(['prefix' => 'index'], function()
{
Route::get('index_top','index\indexController#showIndexTop');
Route::get('top_banner','index\indexController#bannerShow');
...
}
top_banner.php as follow:
<div class="top_banner">
<?php
if($resultcode == 0){
$i = 0;
foreach ($banner["body"] as $info) {
$i++;
if($i==1){
echo "<img src='";
echo( $info['adpic']);
echo "'style='width:1200px;height:145px'/>";
...
The problem are your include statements. Instead of including local views, meaning that PHP would be included and executed in the same context, you're including a URL, which means that a separate request will be sent and result of it's execution will be included. This request doesn't have any contextual information attached like cookies, that would help identify your session, that's why session data is not available.
Replace
include(URL('index/index_top'));
include(URL('index/top_banner'));
with
include('index/index_top.php'); //or whatever local path to index_top.php is
include('index/top_banner.php'); //or whatever local path to top_banner.php is

Codeigniter redirect() does not redirect on some systems

I do have a CI project in which's home function I check, if a cookie already exists. If so, the user should be redirected. This works fine for example on my linux machine (Chrome), but not on my Mac (Chrome) - and I have no clue why.
This is the code:
$session = $this->session->all_userdata();
if(array_key_exists('id', $session)) {
redirect(site_url("loggedin"), "refresh");
} else {
/* set cookie */
Thanks
First, I'm not sure if array_key_exists() is better or if isset($_SESSION['id']) is, but either way CI has this built in with the session->userdata() function, which returns FALSE if the value isn't set:
if($this->session->userdata('session_id'))
redirect('loggedin',"refresh");
//rest of code
Also notice that you don't need site_url() in the redirect() function. CI will do that by default. I'm not sure if any of that has to do with your issue, though. It could be a browser bug. Does redirect('loggedin'); (which would use a location redirect) work?

what is function redirect() in php

I use a code to connect gmail and get my friends list. In that code there is a function call
redirect('https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token='. $oauth->rfc3986_decode($accrss_token['oauth_token']), 'location');
I've searched for function redirect() but didn't find it in the php manual. Is it a built in function in php?
The 2nd parameter is 'location' what is the use of that parameter?
Here is the function where it's used:
public function connect_google($oauth=null){
if(!$oauth)
{
return null;
}
//create a gmailcontacts objects
$getcontact = new GmailGetContacts();
$accrss_token = $getcontact->get_request_token($oauth, false, true, true);
$this->ci->session->set_userdata('oauth_token', $accrss_token['oauth_token']);
$this->ci->session->set_userdata('oauth_token_secret', $accrss_token['oauth_token_secret']);
//redirect to google auth
redirect('https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token='. $oauth->rfc3986_decode($accrss_token['oauth_token']), 'location');
}
It is part of the CodeIgniter URL helper. See:
http://codeigniter.com/user_guide/helpers/url_helper.html
From the documentation:
Does a "header redirect" to the URI specified. If you specify the full site URL that link will be build, but for local links simply providing the URI segments to the controller you want to direct to will create the link. The function will build the URL based on your config file values.
As you've said, it isnt a built in function, so we dont know what it should look like.
However, considering the name i guess it should look like this:
function redirect($url, $header)
{
header("$header: $url");
}
Since sending a Location: {ur} header will redirect your page to another.
Use header: http://php.net/manual/en/function.header.php
header('Location: urlOfDestination');
It is probably a user defined function. It probably works hand in hand with header() hence the name, the first parameter is the page to redirect to, and the second one is to tell the function that it is indeed a redirect to the location. Just check out the header function.

Zend Framework: From a folder (not /)

i notice that when i run a zend framework app from a server, there are alot of side effects. main issue is where i use urls like
/auth/login
i need to use
$this->baseUrl('/auth/login');
thats simple to fix. but when i use
$request->getRequestUri()
for use in redirects. eg after login, i want to redirect the user back to the prev page, it goes to the wrong place. eg. my app root is "http://localhost/app1", $request->getRequestUri() will give /app1. when i try to redirect back, it will goto http://localhost/app1/app1. btw, i am using Zend Server + IIS7 and my app is configured to run from the url stated above. maybe i shld be going to "/" instead. how can i resolve this?
update
this is in my Zend_Form class
// (Zend_Form) Login.php init()
$req = Zend_Controller_Front::getInstance()->getRequest();
$returnUrl = $req->getParam('returnUrl', $req->getRequestUri());
$this->addElement('hidden', 'returnUrl', array(
'value' => $returnUrl
));
// AuthController after login
$returnUrl = urldecode($form->getElement('returnUrl')->getValue());
if (!empty($returnUrl)) {
$this->_helper->getHelper('Redirector')->setGotoUrl($returnUrl);
}
Based on you update:
Its the prependBase-Option in the Redirector what you are looking for:
prependBase: boolean flag indicating whether or not to prepend the base URL when a relative URL is provided
So your fix is:
$this->_helper->getHelper('Redirector')->setGotoUrl($returnUrl, array('prependBase' => false));
If you are using Zend_Application you can use this in your application.ini, and wont need to specify anything else.
resources.frontController.baseUrl = "/your/public/path/"
I have solved this problem with help of Apache configs. In file \usr\local\apache\conf\vhosts.conf find a block with your site and change ways and public folder.

Categories