what is function redirect() in php - 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.

Related

Laravel, redirect unregistered user to login route with parameter

Im using the Laravel base Authentication, when unregistered user attempt to access a link, for example:
http://myapp.com/financial/?email=john#web.com
It is redirect to http://myapp.com/login because the user is not registered yet.
My question is: how do i get the url parameter email of the original link (http://myapp.com/financial/?email=john#web.com) of the user tried access in my login page?
Obs: im using Laravel 5.7
In your controller simply try like that:
$email = request()->input('email'); // null if not set
// do your things
// then redirect
https://laravel.com/docs/5.7/requests#accessing-the-request
If you want to get the URL after the redirect, you can use the referer header of your request. It will contain the address that was being accessed before the redirect.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer
The Referer request header contains the address of the previous web
page from which a link to the currently requested page was followed.
The Referer header allows servers to identify where people are
visiting them from and may use that data for analytics, logging, or
optimized caching, for example.
you can get it easily by accessing $_GET
$email="";
if(isset($_GET['email']) && !empty($_GET['email'])){
$email=$_GET['email'];
}
Laravel provides a function as well:
$email = Input::get('email');
you can use Request object like \Request::segment(1)
or using Input object like Input::get('email', false);
Look in App\Http\Middleware\Authenticate::redirectTo($request). You can customize this to suit your situation. (Laravel docs for Middleware) For example:
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if ($request->has('email')) {
return route('login', ['email' => $request->email]);
} else {
return route('login');
}
}
}

header() redirect not working in my laravel app, using the Evernote cloud SDK

I am using the Evernote Cloud SDK for PHP, which can be found here.
In my code, I am attempting to something akin to their sample OAuth (found here), I have placed the code into a controller:
class OauthController extends Controller
{
public function reqToken()
{
$oauthHandler = new OauthHandler(env('EVERNOTE_SANDBOX', 'true'));
$callback = 'http://localhost/auth'; //temp
$oauthData = $oauthHandler->authorize(env('EVERNOTE_KEY', ''), env('EVERNOTE_SECRET', ''), $callback);
}
}
According to the Evernote Cloud SDK, the following line of code is called under authorize():
header($authorizationUrl);
I have debugged my way to seeing that this line of code does, get called, and yet my page does not get rerouted to the $authorizationUrl, which is:
"Location: https://sandbox.evernote.com/OAuth.action?oauth_token=token"
I end up staying on the blank laravel page.
If it helps, here is my route:
Route::get('/auth', 'OauthController#reqToken');
I've tried looking in the docs for info on the header function, but couldn't see anything that indicates why It's messing up for me. Any ideas?
After the header() function is called, you should kill the request with die; or exit; and not output anything on the page. after your ->authorize() call try adding that in. The other thing it could be is that headers have already been sent, so this isn't "actually" being sent to the browser.
Usually what it is that the header doesn't fully send and Laravel continues doing stuff after your method is called that could corrupt this header or override it and cause an issue. so calling exit; or die; right after the header is set, you skip all of the extraneous overhead that Laravel offers by default.
try laravel redirect function.
redirect()
you can pass in a full url

pass parameters in URL code igniter

I am new to CI and would normally try to pass some information back to a login page like the following...
public function authenticate()
{
$this->load->model('User_model', '', true);
if ($this->User_model->authenticate($this->input->post('username'), $this->input->post('password')))
{
$this->session->set_userdata('loggedin', true);
header('Location: /');
}
else
{
header('Location: /sessions/login/error?='.'1');
}
}
And then on the login page use a _GET.
So how would I best go about this in CodeIgniter?
First of all, using GET for sensitive data like email/password is a general no-no. Bad practice all the way.
But the answer to your question is here - http://www.codeigniter.com/userguide3/libraries/input.html
$this->input->get('username') is equal to $_GET['username'].
You might use $this->input->get('username', TRUE) to escape some malicious code.
And $this->input->post('username') is equal to $_POST['username']. This is what I advise you to use (it requires modification of your HTML though)
From my understanding you want to pass _GET variables for the error codes, right? as i see you are using _POST variables for the form as you should.
As you are working with Codeigniter, you can use the URI structure of the framework and instead of working with _GET work with the MVC structure.
So, instead of redirecting this:
header('Location: /sessions/login/error?='.'1');
Redirect like this:
header('Location: /sessions/login/error/1');
Then in your controller for the login, in the error method:
function error($msgId = 0) {
// here you will get the number you sent in the url as $msgId
}
If you still need to work with _GET variables, that is possible as well. go to the main application config file of the framework /aplication/config/config.php and have a look on the Enable Query Strings section

Codeigniter url redirecting from address url

I have a problem with codeigniter, i can't find a resolv.
How can i make a redirect by parsing into url the destinations url without interpreting the / as a separator betwen controller model etc.
For example:
http://www.example.com/http://www.google.com/?q=test
what it shoud do to thake http://www.google.com/?q=test as a string and put it into
Header("Location: http://www.google.com/?q=test");
any ideeas ?
You can use the url helper's redirect function. If you give it a full address (with http://), it will redirect to that external url.
https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
redirect("controller_name/function_name");
this code is using header function in php
it is equivalent tobase_url() + redirect value
I success make that,
I used url library like has sugest Juul, and the code is like (in controller):
class url extends CI_Controller {
function index() {
# the codeigniter not accept the characters after ? and i make a custom code
# to know where is the ? sign and then i replace http:/ whith two //
$url = str_replace(":/","://",str_replace('%5E','?',$this->uri->uri_string));
#the url var will return http://www.google.com/?q=test
}
}

Redirect toUrl does not work (zf2)

I have a problem with a redirect in Zend Framework 2.
when a user enters a url like http//mysite.test/lorem, he will be redirected to http//mysite.test/news/view/lorem-ipsum.
the code:
$redirecttable = $sm->get('Redirect\Model\RedirectTable');
$route = $redirecttable->getRedirectByRoute($this->params()->fromRoute('page'));
if($route) {
// $route->getToroute() returns "news/view/Lorem-Ipsum"
return $this->redirect()->toUrl( $route->getToroute() );
}
Am I making a stupid mistake here?
The toUrl method redirect to a complete url, but you're sending part of url. You can use $this->redirect()->toRoute($route,$params) for redirecting to a route. reference
If you want to use toUrl method, try this code:
return $this->redirect()->toUrl( $this->getRequest()->getUri()->toString().$route->getToroute() );
Use toRoute() instead of toUrl():
return $this->redirect()->toRoute($route->getToroute())

Categories