Valery Posted February 5, 2013 Share Posted February 5, 2013 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 More sharing options...
Soma Posted February 5, 2013 Share Posted February 5, 2013 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 More sharing options...
Valery Posted February 5, 2013 Author Share Posted February 5, 2013 (edited) 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 February 5, 2013 by Valery Link to comment Share on other sites More sharing options...
Soma Posted February 5, 2013 Share Posted February 5, 2013 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 More sharing options...
Valery Posted February 5, 2013 Author Share Posted February 5, 2013 Sounds neat. And it's definitely easier to implement and more reliable too, since not all browsers send HTTP_REFERER. Link to comment Share on other sites More sharing options...
ryan Posted February 5, 2013 Share Posted February 5, 2013 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>"; } 6 Link to comment Share on other sites More sharing options...
Valery Posted February 6, 2013 Author Share Posted February 6, 2013 Hello Ryan--and thank you for the tip! Link to comment Share on other sites More sharing options...
totoff Posted April 18, 2013 Share Posted April 18, 2013 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 More sharing options...
teppo Posted April 18, 2013 Share Posted April 18, 2013 @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."; 2 Link to comment Share on other sites More sharing options...
totoff Posted April 19, 2013 Share Posted April 19, 2013 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 More sharing options...
ryan Posted April 20, 2013 Share Posted April 20, 2013 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now