Solved

["PHP_SELF"] syntax question?

Posted on 2003-12-01
15
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

617 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