Solved

["PHP_SELF"] syntax question?

Posted on 2003-12-01
15
484 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now