Why does this curPageURL(); not work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Why does this curPageURL(); not work?

Post by simonmlewis »

Code: Select all

function curPageURL() 
{
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
$url = curPageURL();
This is meant to grab all the URL from the page.
The problem I've got is that in the URL that I am grabbing, there will be & signs.

Furthermore, I am posting the grabbed URL and placing it on the end of the current URL. The purpose is to use a text link to post the URL to a form, while keeping the contents of the page, within the page.

So the result with the grabbed URL is something like this:

http://www.thisismydomain.com/index.php ... d1=4&id2=3.

I've only just realised that it won't post half of the URL to the next page because there are & signs..... so it post:

$url = http://www.thisismydomain.com/index.php?page=home

Is there a method around this - apart from using a <form> tag instead?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Why does this curPageURL(); not work?

Post by AbraCadaver »

urlencode() the grabbed url.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why does this curPageURL(); not work?

Post by simonmlewis »

I've done that, but I am assuming I am meant to be seeing the & signs turned into something else - but nothing changes.

Also, with that posting via the URL to the next page, do I just extract it via the $url=$_REQUEST['url'];, and insert it into the 'send friend' form via $url?

I ask all this, because it still only goes up to the first &.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Why does this curPageURL(); not work?

Post by AbraCadaver »

simonmlewis wrote:I've done that, but I am assuming I am meant to be seeing the & signs turned into something else - but nothing changes.

Also, with that posting via the URL to the next page, do I just extract it via the $url=$_REQUEST['url'];, and insert it into the 'send friend' form via $url?

I ask all this, because it still only goes up to the first &.
I don't know what you're doing, but:

Code: Select all

echo 'http://www.thisismydomain.com/index.php?page=home&id1=4&id2=3&url=' . urlencode('http://www.thisismydomain.com/index.php?page=home&id1=4&id2=3');
Gives:
[text]http://www.thisismydomain.com/index.php ... %26id2%3D3[/text]
This will work fine.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why does this curPageURL(); not work?

Post by simonmlewis »

Code: Select all

function curPageURL() 
{
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
$url = curPageURL();
<a href=\"index.php?page=selectorsend&menu=home&bid=$bid&brc=$brc&id1=$id1&id2=$id2&id3=$id3&id4=$id4&id5=$id5&id6=$id6&id7=$id7&id8=$id8&id9=$id9&id10=$id10&id11=$id11&id12=$id12&id13=$id13&id14=$id&url=" . urlencode($url). "\" style='text-decoration: none'><img src='images/btn_sendfriend.png' border='0' /></a>
Does this:
[text]<a href='index.php?page=selectorsend&menu=home&id1=10&id2=109&id3=76&id4=70&id5=&url=http://localhost/phpmyadmin/justbbguns/ ... n&finish=y' style='text-decoration: none'><img src='images/btn_sendfriend.png' border='0' /></a>
[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Why does this curPageURL(); not work?

Post by AbraCadaver »

That link is not valid HTML or PHP.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why does this curPageURL(); not work?

Post by simonmlewis »

May I ask why not?
I have forgotten to show the echo ""; part in the code, but apart from that it looks ok.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Why does this curPageURL(); not work?

Post by AbraCadaver »

simonmlewis wrote:May I ask why not?
I have forgotten to show the echo ""; part in the code, but apart from that it looks ok.
Yeah the echo ""; part makes a difference. Aside from that though, I can't see a problem. I have no idea why you're not getting the correct output. It must be something else (not updating the file when you make changes, working on the wrong code, etc...)

It's odd that in your echo you use double quotes around the href, but the output that you show has single quotes :wink: That's not the output from that code.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why does this curPageURL(); not work?

Post by simonmlewis »

Bingo. I turned it all around. echo ' instead of echo ", so there was no need for \" around the Javascript.

All fine now. Thanks very much.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why does this curPageURL(); not work?

Post by simonmlewis »

Code: Select all

echo '<div style="float: right; text-align: right; width: 305px; margin-right: 10px">
<a href="index.php?page=selectorsend&menu=home&id1=$id1&id2=$id2&id3=$id3&id4=$id4&id5=$id5&url=' . urlencode($url) . '" style="text-decoration: none"><img src="images/btn_sendfriend.png" border="0" /></a>
<a href="javascript:history.back()"><img src="images/btn_remove.png" border="0" /></a>
<a href="index.php?page=selector&menu=home" style="text-decoration: none"><img src="images/btn_restart.png" border="0" /></a>
<br/>Send this to yourself or your friends to review.<br/>Then you or they can come back and make the purchase.</div>';
PROBLEM:
All this now renders is id1=$id1 in the URL, not the dynamic id1=311 (for example).

The 'id's are all REQUESTED at the top of the page. Is it something to do with my ' and " usage??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Why does this curPageURL(); not work?

Post by simonmlewis »

Plan B - and this does work.

Code: Select all

$newurl = urlencode($url);

echo "<div style='float: right; text-align: right; width: 305px; margin-right: 10px'>
<a href='index.php?page=selectorsend&menu=home&id1=$id1&id2=$id2&id3=$id3&id4=$id4&id5=$id5&url=$newurl' style='text-decoration: none'><img src='images/btn_sendfriend.png' border='0' /></a>
<a href=\"javascript:history.back()\"><img src='images/btn_remove.png' border='0' /></a>
<a href='index.php?page=selector&menu=home' style='text-decoration: none'><img src='images/btn_restart.png' border='0' /></a>
<br/>Send this to yourself or your friends to review.<br/>Then you or they can come back and make the purchase.</div>";
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply