Solved

["PHP_SELF"] syntax question?

Posted on 2003-12-01
15
489 Views
Last Modified: 2012-05-04
I understand how the following code works...in that it displays the current filename:

filename.php from the URL as seen below:

ie: http://server.com/<b>filename.php</b>?var1=21&var2=Y&var3=90210


<?php
$currentPage = $HTTP_SERVER_VARS["PHP_SELF"];
?>


<a href="<?php echo printf($currentPage); ?>

what i'm looking to do is display the actual URL with all it's arguments and switches using a ["PHP_SELF"] type variable
ie: http://server.com/<b>filename.php?var1=21&var2=Y&var3=90210</b>

does this make sense? any suggestions?
0
Comment
Question by:nriddock
  • 6
  • 5
  • 4
15 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9852701
hi,

you want to use REQUEST_URI (so $HTTP_SERVER_VARS["REQUEST_URI"] before php 4.1.0 and $_SERVER["REQUEST_URI"]) after

loz
0
 

Author Comment

by:nriddock
ID: 9852889
that works great...one last question...as ive implemented this:


$currentPage = $_SERVER['REQUEST_URI'];

<a href="<?php echo printf($currentPage); ?>?maxRows_Recordset1=25">


it works as desired on the initial click...each subsequent click it appends the url and several clicks later the url looks like this:

http://www.server.com/filename.php?maxRows_Recordset1=25?maxRows_Recordset1=50?maxRows_Recordset1=25?maxRows_Recordset1=100

this is kinda defeats the purpose of the flexibility. do you have any suggestions how to "reset" the url string or limit the number of variables appended to the information returned by $currentPage = $_SERVER['REQUEST_URI'];

thanks
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9853080
i had a feeling you were going to use it for something like this - personally i wouldn't use request_uri for this, i'd have $_SERVER["PHP_SELF"] as before and then loop through the arguments of $_GET to form your link

$link = $_SERVER["PHP_SELF"] . "?";
foreach($_GET as $key => $value) {
  $link .= "$key=$value&";
}
$link = substr($link, 0, -1);

then if you want you can expand and check for a variable that already exists:

$link = $_SERVER["PHP_SELF"] . "?";
foreach($_GET as $key => $value) {
  if($key != "maxRows_Recordset1") {
    $link .= "$key=$value&";
  }
}
$link = substr($link, 0, -1);

loz
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nriddock
ID: 9858700
so do i call /use the above code as follows?

<?php
$link = $_SERVER["PHP_SELF"] . "?";
foreach($_GET as $key => $value) {
  if($key != "maxRows_Recordset1") {
    $link .= "$key=$value&";
  }
}
$link = substr($link, 0, -1);
?>


<a href="<?php echo printf($link); ?>


??? Thanks
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9858799
Almost, you should use the urlencode function, when writing up QUERY_STRINGS
0
 

Author Comment

by:nriddock
ID: 9858935
should it look like this?

<a href="<?php echo printf($link), htmlentities(urlencode($userinput)), '" ; ?></a>

thanks for your help/patience.

-neils
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9859324
no, it should be

<?php

$link = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?";

$_GET['maxRows_Recordset1'] = 25;

foreach($_GET as $key => $val){
    $link .= "$key=" . urlencode($val);
}
$link = substr($link, 0, -1);

<a href="<?php echo $link; ?>">Click Here</a>



0
 
LVL 13

Expert Comment

by:lozloz
ID: 9859532
you forgot the &:

<?php

$link = $_SERVER['PHP_SELF'] . "?";

$_GET['maxRows_Recordset1'] = 25;

foreach($_GET as $key => $val){
    $link .= "$key=" . urlencode($val) . "&";
}
$link = substr($link, 0, -1);

<a href="<?php echo $link; ?>">Click Here</a>

cheers,

loz
0
 

Author Comment

by:nriddock
ID: 9860097
just about there....it works great, with one last item.

i have the option of viewing  25 | 50 | 100 | 250 records per page. i notice the line:

$_GET['maxRows_Recordset1'] = 25;

how would i accomodate for the other record viewing options ie: 25, 50, 100, 250 ?

thanks
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9860269
$_GET['maxRows_Recordset1'] = 25; shouldn't be in there - if you want to display that many rows from your mysql database, you need to limit according to the number in the url variable, e.g. $query = "SELECT * from table LIMIT 0," . $_GET["maxRows_Recordset1"];

loz
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9860271
In that case If you have more than one link to do, I'd encapsulate it into a function.

function make_link($recordset_value);

  $link = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?";

  $_GET['maxRows_Recordset1'] = $recordset_value;

  foreach($_GET as $key => $val){
      $link .= "$key=" . urlencode($val) . '&';
  }
  $link = substr($link, 0, -1);
 
  return $link;
}

For the link it would be then

<a href="<?php echo make_link(25); ?>">click here</a>

0
 

Author Comment

by:nriddock
ID: 9860569
aolXFT

it's giving me a :
Parse error: parse error, expecting `'{'' in /var/www/html/php/local/filename.php on line 132

here is the code I've entered :
<?php
function make_link($recordset_value);{ ---------------------this is line 132------------------
  $link = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?";
  $_GET['maxRows_Recordset1'] = $recordset_value;
  foreach($_GET as $key => $val){
      $link .= "$key=" . urlencode($val) . '&';
  }
  $link = substr($link, 0, -1);
  return $link;
}
?>
do you see where I would be missing a {
0
 
LVL 13

Accepted Solution

by:
lozloz earned 500 total points
ID: 9860754
it should be

function make_link($recordset_value) {

loz
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9860756
Ah sorry used a ; instead of a {

change it to function

function make_link($recordset_value){
0
 

Author Comment

by:nriddock
ID: 9861456
everything is working great...

lozloz ill award you 500 pts here and
aolXFT ill post 500 pts for you in: Home|All Topics|Web Development|Web Languages|PHP
The post will be labled ptys for aolXFT


thanks for you help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question