ramasaig — 2014-05-27T11:59:04-04:00 — #1
I'm trying to create a link (which I shall put in an e-mail) that will direct to a specific part of the target page.
My site uses a page template, and the content files are inserted using a PHP 'require' statement. Such statements won't take query strings or (I discover) bookmarks. Thus "require_once 'targetfile.php#bookmark'" results in an error.
(whereas "require_once 'targetfile.php'" works as expected.
If I write an absolute URL the template page is by-passed, and the content is displayed with no styling.
I'm trying to think of a workaround. Can anyone help, please.
felgall — 2014-05-27T15:12:34-04:00 — #2
Strip off the #bookmark to use in the require. The #bookmark is used by the browser after the page loads while you are using the part before that to determine what to load.
ramasaig — 2014-05-27T17:40:26-04:00 — #3
Thank you, felgall.
I think I have expressed the title of my thread poorly. I know that the 'require' won't work with the bookmark, and why.
A better title might have been: "How do I get a bookmark through a PHP 'require' statement ?"
What I'm trying to work out is how to access the page in such a way that the visitor is taken to the bookmarked part of the page, just as they would be with a normal link with no PHP involved. I can't write a regular URL such as 'http://example.com/path/pagename.php#bookmark' because that will bypass the template and the page will be displayed with no styling (and I will shortly be adding some code to prevent that happening too).
I do have the bookmark available as a PHP variable, so perhaps some JS to focus on the element with id='bookmark' could be used. But it's a fair bit of work for one bookmark.
fretburner — 2014-05-27T19:32:41-04:00 — #4
I'm a little confused as to what the problem is. The fragment (the # symbol and anything that comes after) is not passed to the server, it's only used within the browser, so it can't affect what happens on the server.
To use your example, if a user navigates to: http://example.com/path/pagename.php#bookmark, the server will load pagename.php and send the output back to the user's browser. Once the page has been received, the browser will scroll to the element with the ID
ramasaig — 2014-05-27T20:56:01-04:00 — #5
Yes, what you say is absolutely correct. My problem is that a URL like 'example.com/path/filename.php#bookmark' will bypass my index.php control file and the page template, so the page gets displayed with NO styling. My content 'pages' are not complete HTML pages in their own right, they are just scripts (often with lots of plain HTML text and image content, but no DOCTYPE or head).
My control file, together with .htaccess ensures that the site navigation loads all page content into the template using the PHP 'require'. But as is well established this will not work if there's a query string or bookmark on the end of the content file name. Therefore if I want to give someone an absolute URL to a bookmarked place I have a problem.
I have to 'extract' the bookmark, load the contents file using PHP 'require' and then make the bookmark available to the HTML page when it has loaded.
I'm working on a solution in which the control script strips off the bookmark and turns it into a PHP variable which is loaded into a hidden input in the template. I then use JS and a couple of lines of jQuery to scroll the page to the bookmark ID (given by the value of the variable). It works (in prototype), but it may cause me problems in mod_rewrite (.htaccess). I had hoped to find something simpler.
fretburner — 2014-05-27T21:08:58-04:00 — #6
Could you give an example of a working URL that does load one of your content pages correctly? If you could also share the code that does the routing and requires the chosen content page, as that might also be useful.
This is the part that I'm struggling to understand. Your server (and therefore your .htaccess file) never sees the fragment, so it should be impossible for it to interfere with the require statement.
ramasaig — 2014-05-28T06:35:39-04:00 — #7
I had no idea when I started this thread that it would be so difficult to explain what I saw as my problem, but your questions have made me re-examine my assumptions.
This link will open the 'Terms and conditions' page of a B&B web site. http://www.mull-bed-and-breakfast.co.uk/terms. .htaccess creates a query string, index.php parses that and attaches the correct argument to the PHP 'require' statement.
So far so good, but what happens when I want to direct the visitor to the paragraph at the bottom of that page about dogs (for example) ? I made the assumption that I couldn't just add the bookmark as in http://www.mull-bed-and-breakfast.co.uk/terms#dogs. Just to be sure I tried it, and it didn't work, thereby confirming my assumption that .htaccess would choke on it.
However having persevered with this issue since my last posting, it looks to me as if the reason it didn't work was a typo. I hope that if you click the second link above it will now take you to the intended place ? If it doesn't I'm back to square one.
It seems to work OK for me, which means my work with the <input> and JS was unnecessary (although that did work too), so I expect I learnt something.
While investigating I've also added rules to .htaccess to prevent pages being loaded outside the template, so something useful has come out of it.
Thank you for your help.
fretburner — 2014-05-28T07:31:48-04:00 — #8
Yeah that link works fine for me and scrolls the page to the correct location. Nice site, by the way. I had a browse through the gallery, it looks quite idyllic there!
ramasaig — 2014-05-28T09:58:03-04:00 — #9
Thanks for your help and kind words about our B&B site. Yes, it's nice here, but it can be very wet and windy in winter. Rarely very cold, I'm not sure we even had a frost last winter..