Jump to content

Getting Page by httpUrl?


Valery
 Share

Recommended Posts

Hi guys,

A quick question: I am trying to find a page by its URL. 

$wire->pages->find("httpUrl={$_SERVER['HTTP_REFERER']}");

This does not work and PW says Error Uncaught exception 'WireException' with message 'Field does not exist: httpUrl'

Does anyone know of an elegant solution to find a page id by its httpUrl?

Thanks.

Link to comment
Share on other sites

The path can also be used to get a page. Find doesn't work or make much sense with url fields I think.

$p = $pages->get("/url/to/page/");
if($p->id) echo "found: $p->id $p->title";
Url would be from the root and without protocol.

I don't know what you're trying. Are you using Boostrap index.php? Also not sure why HTTP_REFERER.

Link to comment
Share on other sites

Thanks, Soma.

I am developing a site where I use some self-made scripts to process forms from PW pages.

PW displays a page with a file upload form. When submitted, this form is processed by a custom script which uploads the file and puts a link to it into the page. When done, the script outputs a header with the URL of the file upload page. This way the file does not get uploaded twice when the user presses F5 by mistake.

So I need my script to know the ID of the page from which the form was submitted so that it could redirect the user back to it.

I use the get("/url/to/page") method to get the referrer page ID, and it works. It's just that today I came upon the httpUrl thing and thought whether or not this might be useful for my task.

Addition: here's how I find the referring page's ID:

$pagePath = str_replace($_SERVER['HTTP_ORIGIN'], "", $_SERVER['HTTP_REFERER']);
$refererPageId = $wire->pages->get("{$pagePath}");
Edited by Valery
Link to comment
Share on other sites

Why not just add a hidden field to the form with the path or id of the page.

<input type="hidden" name="refpageid" value="<?php echo $page->id?>"/>

Then use

$p = $pages->get((int) $input->post->refpageid);
Link to comment
Share on other sites

Some $_SERVER variables like HTTP_REFERER can be manipulated by the client, so it's probably not safe to utilize without sanitization. However, since you are trying to keep track of the last page, I'd suggest using the $session variable. Place this at the top of your page before output:

if($session->referrer_id) $page->referrer_page = $pages->get($session->referrer_id); 
$session->referrer_id = $page->id; 

Now anytime you want to access the last page, you'd do this:

if($page->referrer_page) {
  echo "Last page you visited was: "
  echo "<a href='{$page->referrer_page->url}'>{$page->referrer_page->title}</a>";
}
  • Like 6
Link to comment
Share on other sites

  • 2 months later...

hi,

i came across this thread while looking for a way to build a "link to previous page" and it seems to solve my problem. so my question is more or less meant to help me to better understand processwire: is the function ($session->referrer_id) somewhere documented? i can't find it neither in the cheatsheet nor in the api.

sorry if this is a silly question. i'm new to everything that has to do with sessions in php.

Link to comment
Share on other sites

@totoff: that was just the name of the variable Ryan used in his example. See how he checks and sets it in the first code block?

You can store any custom variables you might need in $session and then use them on any other page for that particular (user-specific) session:

$session->referrer_id = $page->id; 
$session->interesting_fact = "Chuck norris can slam a revolving door.";
  • Like 2
Link to comment
Share on other sites

hi teppo,

thank you very much for your reply. i think i got it but just to make sure i would like to confirm what i understand:

given we have three pages and flip from page a to b to c the referrer_id is first set on page a and holds the page id of a:

($session->referrer_id = $page->id)

than it can be called on page b because it's stored in the user session. on page b the value is reset to the id of page b thus it can be called on page c and so on. is that right so far?

thanks for helping a coding newbie!

Link to comment
Share on other sites

than it can be called on page b because it's stored in the user session. on page b the value is reset to the id of page b thus it can be called on page c and so on. is that right so far?

Sounds like you've got it right. 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...