Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

An if statement based on what the url contains

Status
Not open for further replies.

gameon

Programmer
Feb 23, 2001
325
GB
Hi, usually I use ColdFusion, but at the mo it has to be php.

For navigation - I usually use the following to make naviagtion maintainance easier.

if the url contains "a string"

do this:

else do this:

Can someone help me do this with php?

Cheers if you can illuminate me...

an example to change an image would be:

<if the url contains &quot;/help&quot;>help<else>nothelp</if>.gif

M@)
 
It could be a long list of if's if you really go mad, switch is fatser and easier to manage :

<?php
// search the url for help test or admin
switch ($searchword) {
case help:
preg_match(&quot;/$searchword/i&quot;,$REQUEST_URI);
print &quot;found HELP in URL&quot;;
break;

case test:
preg_match(&quot;/$searchword/i&quot;,$REQUEST_URI);
print &quot;found TEST in URL&quot;;
break;

case admin:
preg_match(&quot;/$searchword/i&quot;,$REQUEST_URI);
print &quot;found ADMIN in URL&quot;;
break;
}
?> ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
PHP provides the information need in an associative array. In versions earlier than 4.1.0, the array is called $_HTTP_SERVER_VARS. In 4.1.0 and higher, both that array and the array is $_SERVER are available. $_HTTP_SERVER_VARS has been deprecated in favor of $_SERVER.

If you have the PHP runtime configuration directive register_globals set to on, then the values will also be available as separate variables.

(for more information on this and the other arrays available to you, I direct your attention to )

You need to look at the values that are provided in the arrays $_SERVER and $_GET.



To match part of the string, you can use the regular expression functions. (direct your browser to for full information).


To match your substring, you might use:

if (preg_match (&quot;/\/help/&quot;, $_SERVER[&quot;REQUEST_URI&quot;])
{
print &quot;got help&quot;;
}
else
{
print &quot;got no help&quot;;
}
______________________________________________________________________
Don't say thanks. Just award stars.
______________________________________________________________________
 
Hi, and thanks so much for your help.

Just to confirm, I will be using this to dynamically name an image.

In cold fusion, to dynamically output (server side) the image to welcome_home.gif or welcome_nothome.gif I would do:

welcome<cfif statement>_home<cfelse>_nothome</cfif>.gif

when you use print, is that the same?

M@)
 
PHP does not operate as meta-tags. It's more procedural.

Here's a functional equivalent in PHP. This is my own codeing style -- I'm sure KarveR's will differ somewhat typographically, but will likely be the same at least in the use of looping constructs.

<?php

print &quot;welcome&quot;;
if (statement)
{
print &quot;_home&quot;;
}
else
{
print &quot;_nothome&quot;;
}
print &quot;.gif&quot;;

?> ______________________________________________________________________
Don't say thanks. Just award stars.
______________________________________________________________________
 
Can I rewrite this as:

<?php print &quot;welcome&quot;;if (statement){print &quot;_home&quot;;}else{
print &quot;_nothome&quot;;}print &quot;.gif&quot;;?>

M@
 
Is there a reason for this not to work?

<a href=&quot;/jogme/registration/&quot;><img src=&quot;/jogme/images/left_nav/<?php if (preg_match (&quot;/\/registration/&quot;, $_SERVER[&quot;REQUEST_URI&quot;]){print &quot;down/registration.gif&quot;;}else{print &quot;up/registration.gif&quot;;}?>.gif&quot; border=&quot;0&quot; width=&quot;173&quot; height=&quot;13&quot; alt=&quot;Registration&quot; name=&quot;<?php if (preg_match (&quot;/\/registration/&quot;, $_SERVER[&quot;REQUEST_URI&quot;]){print &quot;registration&quot;;}else{print &quot;&quot;;}?>&quot; onMouseOut=&quot;MM_swapImgRestore()&quot; onMouseOver=&quot;MM_swapImage('registration','','/jogme/images/left_nav/over/over_registration.gif',1)&quot;>

M@
 
I like to keep things spaced abit for ease of debugging.
You have missed the second bracket on each of the preg_match statements.

<a href=&quot;/jogme/registration/&quot;>
<img src=&quot;/jogme/images/left_nav/

<?php
if (preg_match (&quot;/\/registration/&quot;, $_SERVER[&quot;REQUEST_URI&quot;])){
print &quot;down/registration.gif&quot;;
}else{
print &quot;up/registration.gif&quot;;
}
?>

.gif&quot; border=&quot;0&quot; width=&quot;173&quot; height=&quot;13&quot; alt=&quot;Registration&quot; name=&quot;

<?php
if (preg_match (&quot;/\/registration/&quot;, $_SERVER[&quot;REQUEST_URI&quot;]))
{
print &quot;registration&quot;;
}else{
print &quot;&quot;;
}
?>

&quot; onMouseOut=&quot;MM_swapImgRestore()&quot; onMouseOver=&quot;MM_swapImage('registration','','/jogme/images/left_nav/over/over_registration.gif',1)&quot;> ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Ahh yes, much clearer.

Thank you soo much. If any of you helpful people ever need some flash stuff (that I am suitable for), corp or otherwise please let me know:
( - latest)

matt@3ainteractive.com

M@)
 
Hi again,
when I put the code in the page, it just wrote it out:

(4K)

Using the code:
--------------------------------------------------------

<a href=&quot;/~mattg/registration/&quot;>
<img src=&quot;/~mattg/images/left_nav/

<?php
if (preg_match (&quot;/\/~mattg/registration/&quot;, $_SERVER[&quot;REQUEST_URI&quot;])){
print &quot;down/registration.gif&quot;;
}else{
print &quot;up/registration.gif&quot;;
}
?>

.gif&quot; border=&quot;0&quot; width=&quot;173&quot; height=&quot;13&quot; alt=&quot;Registration&quot; name=&quot;

<?php
if (preg_match (&quot;/\/~mattg/registration/&quot;, $_SERVER[&quot;REQUEST_URI&quot;]))
{
print &quot;registration&quot;;
}else{
print &quot;&quot;;
}
?>

&quot; onMouseOut=&quot;MM_swapImgRestore()&quot; onMouseOver=&quot;MM_swapImage('registration','','/~mattg/images/left_nav/over/over_registration.gif',1)&quot;>
</a>

-----------------------------------------------

Have I not closed the comments properly?

M@
 
In this section:
print &quot;down/registration.gif&quot;;
}else{
print &quot;up/registration.gif&quot;;}?>
.gif&quot; border=&quot;0&quot; width=&quot;173&quot; height=&quot;13&quot;.

The page will be looking for the image registration.gif.gif.

Remove the additional '.gif' to sort this.
'Does my thumb look big in this?'
homebut.gif
elshtroll
 
Fixed that, but it's still not having any of it.

To make it clearer, I have put the whole statement in:


----------------------------------------------------

<?php
if (preg_match (&quot;/\/~mattg/registration/&quot;, $_SERVER[&quot;REQUEST_URI&quot;])){
print &quot;<img src=&quot;/~mattg/images/left_nav/down/registration.gif border=&quot;0&quot; width=&quot;173&quot; height=&quot;13&quot; alt=&quot;Registration&quot; name=&quot;registration&quot; onMouseOut=&quot;MM_swapImgRestore()&quot; onMouseOver=&quot;MM_swapImage('registration','','/~mattg/images/left_nav/over/over_registration.gif',1)&quot;>&quot;;
}else{
print &quot;<img src=&quot;/~mattg/images/left_nav/down/registration.gif border=&quot;0&quot; width=&quot;173&quot; height=&quot;13&quot; alt=&quot;Registration&quot; name=&quot;registration&quot; onMouseOut=&quot;MM_swapImgRestore()&quot; onMouseOver=&quot;MM_swapImage('registration','','/~mattg/images/left_nav/over/over_registration.gif',1)&quot;>&quot;;
}?>
------------------------------------------------


I'll keep trying to fiddle...

M@
 
PHP will not like the double quotes in the print statement.

The line:
print &quot;<img src=&quot;/~mattg/images/left_nav/down/registration.gif border=&quot;0&quot; width=&quot;173&quot; height=&quot;13&quot; alt=&quot;Registration&quot; name=&quot;registration&quot; onMouseOut=&quot;MM_swapImgRestore()&quot; onMouseOver=&quot;MM_swapImage('registration','','/~mattg/images/left_nav/over/over_registration.gif',1)&quot;>&quot;;

should read:
print &quot;<img src=\&quot;/~mattg/images/left_nav/down/registration.gif\&quot; border=\&quot;0\&quot; width=\&quot;173\&quot; height=\&quot;13\&quot; alt=\&quot;Registration\&quot; name=\&quot;registration\&quot; onMouseOut=\&quot;MM_swapImgRestore()\&quot; onMouseOver=\&quot;MM_swapImage('registration','','/~mattg/images/left_nav/over/over_registration.gif',1)\&quot;>&quot;;

The one in Bold was missing.
The double quotes makes php believe that it's the end of the print function.

'Does my thumb look big in this?'
homebut.gif
elshtroll
 
Sorry should have said.

each double quote &quot; has to have a \ infront of it, when it's within the print quotes.
'Does my thumb look big in this?'
homebut.gif
elshtroll
 
The following does work:


<?php
if (preg_match (&quot;/\/registration/&quot;, $_SERVER[&quot;REQUEST_URI&quot;])){
print &quot;bosh&quot;;
}else{
print &quot;dosh&quot;;
}?>


but this doesn't.

Are their rules about what can go in the expression?

<?php
if (preg_match (&quot;/\/~mattg/\/registration/&quot;, $_SERVER[&quot;REQUEST_URI&quot;])){
print &quot;bosh&quot;;
}else{
print &quot;dosh&quot;;
}?>

What about having nested &quot;&quot;, like above?

I think I need me a book...

M@
 
I have finnaly got it - nearly - with lost of help.

But the URL detect is not working...

<?php
if (preg_match (&quot;/\/home/&quot;, $_SERVER[&quot;REQUEST_URI&quot;])){
print &quot;yes&quot;;
}else{
print &quot;no&quot;;
}?>

The above outputs no, even when I am in a url that contains the string: &quot;/home/&quot;

Is the syntasx correct?

M@

 
I couldn't get anything out of $_SERVER[&quot;REQUEST_URI&quot;] on php 4.2.0 so I guess thats one of the things that may or may not be supplied by the server in later versions :-(

As the manual page says:
$_SERVER
Variables set by the web server or otherwise directly related to the execution environment of the current script. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).

Check if you can get this from $HTTP_SERVER_VARS[REQUEST_URI], if so you should be in business. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR, I'm running PHP 4.2.1, and $_SERVER[&quot;REQUEST_URI&quot;] is available and works as advertised. ______________________________________________________________________
My God! It's full of stars!
______________________________________________________________________
 
Guess *I* need a slight upgrade then. [lol] ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top