php how to pass an anchor in the command line - php

I need to pass parameters and an anchor in a php program to make sure the cursor is positioned at the last place it was in the page when it returns.
program.php?ID=123&ID2=456#777
ID=123
ID2=456
html anchor is #777
Can someone tell me how to make this work? Since it isn't working the way I'm doing it now. Thanks

The browser will not send the anchor to web server.
So PHP can't get the anchor from url.
maybe you can use javascript to request program.php?ID=123&ID2=456&anchor=777, then you can get the anchor by $_GET['anchor'].
Sorry for my bad english (愒o愒).
If you want force the cursor to the position.
this code will redirect to http://website.com/page.php#anchor and the broswer will auto cursor the position.
<?php
header("location: http://website.com/page.php#anchor");
exit;
warning: take care about header function, there is the manual header function
If the current page is the same as the redirect page, above the code will infinite redirect and the browser will throw error. So you have to write some logic, make it redirect when you want. Or just write two different page, one to show content, other one redirect to content page with anchor.
But I still think it is better to do that by JavaScript.

Related

Using anchor tags to load php

How can I use anchor tags to load a certain php function?
Such as the default php function is Firstpage(); and that function loads a list of links, such as Secondpage and Thirdpage, in which direct to domain.com/index.php#secondpage and etc
To cut it short (I didnt really finish the top part, sorry), how can I make it so if the link is #secondpage, load the secondpage function, whilst not having firstpage loaded unless the domain has no anchor tags?
Or is there another way?
Sorry for this, Its hard for me to explain :P
You can't do that in plain PHP. Javascript will also be needed for that.
Or is there another way?
Yes
Or, if this is not possible, having the load a function in php
Simple, use query string values. For example
test 1
Then in PHP you can do
if(isset($_GET['function'])){
$function=intval($function);
if($function==1){
//call first page function here
}
elseif($function==2){
//call second page function here
}
}
else{
//since we got no indication from the URL, we can show default page here.
}

Detecting the current page from an included page

In my pages I have a top navigation bar.
I used to have this in the pages themselves but I've decided its better practice to have it in a seperate header file and then include this in every page.
The trouble is this has somewhat ruined my if server php self is whatever.php then class=current page on the nav bar.
Is there a way to detect the current page from within a header?
If I understand you correctly, you can use:
basename(__FILE__);
This will return the name current script that is being executed.
Without the extension:
substr(basename(__FILE__), 0, -4);
the way I found to do it was in my main page itself to set a variable
$current="whatever";
just before I run the header and then in the header check against the current variable

Can I modify anchor tag href in htaccess?

Not sure if this is possible but wanted to know if htaccess has a trick...
Say I have anchor tag href like this -
Click here
I have changed the URL structure using mod_rewrite but wanted to know if i can hide the actual URL in href using htaccess.
i.e when a user hover over the anchor tag, the browser displays http://example.com/index.php?id=12345 at the bottom. All I want the browser to display is http://example.com/index/12345 without changing it manually on all my pages.
Thanks for the help !
Why don't you change the link to the following?
Click here
As you can change the .htaccess I expect that you own or adminstrate this domain. So it should be possible.
If the links are generated by PHP code, then I suggest you to implement and use a translation function like:
function beautify($ugly) {
// your logic comes here
return $nice; // ;)
}
... and wrap it around the existing code that currently outputs the urls. This would have two advantages:
It's easy and more failsafe to migrate to the new url scheme
From now on you have control over all url related code using a single function
I agree, htaccess can't help you. I guess you'll have to change them manually.
I wish I could be of more help
No. htaccess is for processing input to the web server, not data sent back from the server.
If you use jQuery you could have it rewrite the href when the page loads using something like this.
$(function(){
$("a").each(function() {
this.href = 'some_new_url that you made based on the one in this.href';
});
});

PHP Header Location Change of Parent Frame

I have an iFrame that does some background processing. When this processing is complete I would like to re-direct the user to another page, but the header change code is only affecting the embedded iFrame. Is there a way to target the main window?
I have seen the deprecated Meta redirect have a target attribute, but I don't know how widely it is supported.
In Javascript:
top.location.href = "resultpage.htm";
Works only if the top frame is on the same domain as the emitting page.
For a solution that works across domains and without Javascript, the following would work:
Continue
Use JavaScript to track content of frame, if content change, redirect browser :)
We can use javascript like this:
target.window.location='locationpage.php';
parent.window.location='index.php';
For me this always works without fail, have tried many of the others but there always seems to be some sort of issue....and it does not matter if headers have been sent etc.....
<?php
echo "<script>window.location = 'http://www.google.com'</script>";
?>
Remember this goes at the very bottom

using getHtmlSource in Selenium after following a link on the initial page

I opened a page and am following a couple of links with the click() method.
$this->selenium->open("test.html");
$this->selenium->click("link=testlink1");
$this->selenium->waitForPageToLoad("10000");
$this->selenium->click("link=testlink2");
$this->selenium->getHtmlSource();
Now I want to get the HTML source of the current page that I am on, but getHtmlSource seems to only get the source of the initial page from the open() call.
How do I get the HTML source of the page from 'testlink2'? The last link I followed and the current page I'm on.
Ok, so it appears if you do this, things work.
$link2 = $this->selenium->click("link=testlink2");
$this->selenium->getHtmlSource($link2);
Now, I'm running into a problem that getHtmlSource doesn't seem to returning everything. Looks like it has some sort of buffer limit :(
Also, it doesn't look like this technique will work on links to pages that require authentication. So if you login first, then click on some links, it doesn't work.
getHtmlSource should return the current page's HTML source. Your example might need an additional waitForPageToLoad between clicking the link and getting the page source.

Categories