Kohana 3.2 - Get referrer URI - php

I'm attempting to get the referrer URI in Kohana 3.2 using the following code:
$referrer = $this->request->referrer();
var_dump($referrer);
However the function returns NULL, I'm expecting it to return the page I left to get to this one.
Is that how it should work... and if so what am I missing or doing wrong?
This is being run in the Controller.
Documentation here: http://kohanaframework.org/3.2/guide/api/Request#referrer

The issue I was having was cause by the way my Kohana system is setup. Basically I have a routeHandler in the middle of my system which controls the routes users take. In that script a new request was generated and then executed.
What was happening was when creating the new request object it wasn't having the referrer uri added to it. I have added the following code and now I am able to get the referring uri from the controller at the end of the routing process.
$referrer = $this->request->referrer();
$request = new Request($uri);
$request->referrer($referrer);
echo $request->execute()
->send_headers()
->body();
Alternatively a cleaner more native solution as suggested by atma is the following:
Request::initial()->referrer()

Related

Phalcon response object is not working

I cannot get the response object to work and I have tried many implementations referenced by the documentation. I don't have an external configuration for the response object, only what is in the code below.
When this code is run an empty page is displayed (there is no view linked with this action). I get no error messages in the browser / apache / php logs either.
I've checked the path to the controllers directory, which is located at:
"__ DIR __ . '/../controllers/'"
My code:
public function sendAction()
{
$this->response->redirect("contact/index");
}
If I place the following code at the top of the method, I see the expected page...
echo phpInfo();
die();
I have tried all combinations below:
$this->response->redirect("contact/index");
// Example shown in Phalcon Docs
$this->response->redirect("http://en.wikipedia.org", true);
return $this->response->redirect("http://en.wikipedia.org", true);
// Should navigate to the base URI
$this->response->redirect();
return $this->response->redirect();
// Creating a new instance of \Phalcon\Http\Response
$resp = new \Phalcon\Http\Response();
$resp->redirect("http://en.wikipedia.org", true);
I'm really not sure what else to try. I have scoured forums and documentation but haven't found anything that solves my issue.
Yes this question has been asked before but the answer didn't help!
I'm running Phalcon 2.0.1 | PHP 5.6.27 | Chrome v.54 | OSX 10.12
My opinion is that the code isn't wrong, but something must not be configured properly. I have tried installing php and Phalcon a couple of times, neither went smoothly and I downloaded a number of required packages and missing elements as I came across them trying to install. I then backed up my data and wiped my hard drive to start fresh, but the problem persists.
I had the same issue. Even if the official documentation makes examples like this
// Redirect to the default URI
$response->redirect();
// Redirect to the local base URI
$response->redirect("posts/index");
// Redirect to an external URL
$response->redirect("http://en.wikipedia.org", true);
// Redirect specifying the HTTP status code
$response->redirect("http://www.example.com/new-location", true, 301);
The only way it worked for me was returning the response send method. I hope it solves your problem too.
$this->response->redirect("http://en.wikipedia.org", true, 302);
return $this->response->send();

Modify request path info

Now, let me guess what y'all may be thinking... "It's a bad idea to modify the path info before it's processed. Why would you ever want to do that? This is malicious behavior!!!"
I am trying to get a controller/action representation of my previous URL; gotten through Yii::app()->getRequest()->getUrlReferrer().
From Yii 2 issues, it's possible to set the path info for a new request and parse that request. However, from the Yii 1 source, the only methods which deals with the path info are getPathInfo() and decodePathInfo(). If there was a setPathInfo(), I could have used that and the urlManager->parseUrl() to achieve this. But we aren't allowed to set the path info.
How can I arrive at a controller/action representation of my previous URL?
Using PHP $_SERVER['HTTP_REFERER'] it's good way to find previous location but will give you incomplete url.
You can try this way in Yii 1.0 -
if your url like - www.domain.com?r=site/page
if(isset($_REQUEST['r']) && !empty($_REQUEST['r'])){
$previous_location = $_REQUEST['r'];
Yii::app()->user->setState('previous_location', $previous_location);
}
Another way-
$controller_name = Yii::app()->controller->id;
$action_name = Yii::app()->controller->action->id;
Yii::app()->user->setState('previous_location', $controller_name.'/'.$action_name);
so you can find out your previous location by -
echo Yii::app()->user->getState('previous_location');
It's may be help you to resolve your issue.
Yii does not allow the CHttpRequest object live past the parsing of the routes. And creating a new CHttpRequest is impossible after the app is created.
I realized the only way to go about this is the vanilla Yii::app()->controller->action object. From this, I could get the module, controller and action ID for the specific URL.

SimplePie through CraftCMS adding #? to feed URL

Okay - simple details first:
Feed URL: http://feeds.wired.com/wired/index
When I access this directly through simplepie and not CraftCMS, the feed loads without issue. When I access it through the CMS template system it throws an error saying the returned XML is effectively garbage. Looking into this, its because the feed host is returning a page not found.
I have dug through the code to find out why it is a page not found on a valid URL and found that in the file:
/library/simplepie/registry.php
public function &create($type, $parameters = array()) (around line 160)
Now in this function I see that a new reflection class is created and new InstanceArgs are sent the parameters. At this point the Feed URL gets changed from:
http://feeds.wired.com/wired/index
to
http://feeds.wired.com/wired/index?#
Effectively this new URL with the added ?# on the end is causing the feed site to return a page not found. And hence all my errors...
This is beginning to hit the end of my abilities - and I would like to know what is causing the ?# to be added and if there is a way to prevent it? Or any other possible solutions to my problem.
I can't explain why it works outside of Craft and not inside, but it definitely looks like a SimplePie bug to me.
Just created an issue and pull request with the fix for them (https://github.com/simplepie/simplepie/issues/366) and will see about updating it in Craft itself.
Update: looks my fix didn't broke some of their tests, so we won't be putting that into Craft, but they have acknowledged it needs to be fixed.

Handling complex GET parameters with Slim Framework HTTP routing

Assuming the following naive code:
$app->get( '(/store(/:url)+)', function( $url = NULL ) use ( $app )
{
// Execute actions on $url
});
The above works fine for the following:
http://localhost/api/0001/store/url-data
But it fails for:
http://localhost/api/0001/store/http%3A%2F%2Fexample.com%2FSomething
http://localhost/api/0001/store/http://example.com/Something
// and other variations
I wish to pass a full encoded URI to handle on the server side. How can I manage to do that with Slim?
Notes:
Other types of HTTP Requests (POST, PUT) won't work for this given problem.
It can be solved on the other end by reformatting (serializing) the URI, but I wish this to be my last resort.
Important Edit - Answer
So it turns out the above is a bug in the framework which is currently being tested and hopefully fixed and released soon. I solved the problem temporarily by serializing the URI before it reaches the server side.
So it turns out the above is a bug in the framework which is currently being tested and hopefully fixed and released soon. I solved the problem temporarily by serializing the URI before it reaches the server side.

How do I get the final, redirected, canonical URL of a website using PHP?

In the days of link shorteners and Ajax, there can be many links that ultimately point to the same content. I was wondering what the best way is to get the final, best link for a web site in PHP, hopefully with a library. I was unable to find anything on Google or GitHub.
I have seen this example code, but it doesn't handle things like a rel="canonical" meta tags or default ssl ports: http://w-shadow.com/blog/2008/07/05/how-to-get-redirect-url-in-php/
Facebook seems to handle this pretty well, you can see how they follow 301's and rel="canonical", etc. To see examples of the way Facebook handles it, use their Open Graph tool:
https://developers.facebook.com/tools/debug
and enter these links:
http://dlvr.it/xxb0W
https://twitter.com/#!/twitter/statuses/136946408275193856
Is there a PHP library out there that already has this pre-built, where it will check for these headers, resolve 301 redirects, parse rel="canonical", detect redirect loops and properly just grab the best resulting URL to use?
As an alternative, I am open to APIs that can be used, but would prefer something that runs on my own server.
Since I wasn't able to find any libraries that really did what I was looking for, and I was hoping to do more than just follow HTTP redirects, I have gone ahead and created a library that accomplishes the goals and released it under the MIT license. You can get it here:
https://github.com/mattwright/URLResolver.php
URLResolver.php is a PHP class that attempts to resolve URLs to a final, canonical link:
Follows 301 and 302 redirects found in HTTP headers
Follows Open Graph URL <meta> tags found in web page <head>
Follows Canonical URL <link> tags found in web page <head>
Aborts download quickly if content type is not an HTML page
I am certainly not an expert on the rules of HTTP redirection, so if anyone has suggestions on how to improve this library, it would be greatly appreciated. I have tested in on thousands of URLs and it seems to do pretty well. I followed Mario's advice and used PHP Simple HTML Parser library where needed.
Using Guzzle (a well known and robust HTTP client) you can do it like that:
<?php
use Guzzle\Http\Client as GuzzleClient;
use Guzzle\Plugin\History\HistoryPlugin;
public function resolveUrl($url)
{
$client = new GuzzleClient($url);
$history = new HistoryPlugin();
$client->addSubscriber($history);
$response = $client->head($url)->send();
if (!$response->isSuccessful()) {
throw new \Exception(sprintf("Url %s is not a valid URL or website is down.", $url));
}
return $response->getEffectiveUrl();
}
I wrote you a little function to do it. It's simple, but it may be a starting point for you. Note: the http://dlvr.it/xxb0W url returns an invalid URL for it's Location response header.
You'll need the Altumo PHP library for it to work. It's a library that I wrote, but it's MIT license, as is this function.
See: https://github.com/homer6/altumo
Also, you'll have to wrap the function in a try/catch.
/**
* Gets the final URL of a URL that will be redirected.
*
* #param string $url_string
* #throws \Exception //on error
* #return string
*/
function get_final_url( $url_string ){
while( 1 ){
//validate URL
$url = new \Altumo\String\Url( $url_string );
//get the Location response header of the URL
$client = new \Altumo\Http\OutgoingHttpRequest( $url_string );
$response = $client->sendAndGetResponseMessage();
$location = $response->getHeader( 'Location' );
//return the URL if no Location header was found, else continue
if( is_null($location) ){
return $url_string;
}else{
$url_string = $location;
}
}
}
echo get_final_url( 'your url here' );
Please let me know if you'd like further modifications or help getting it going.

Categories