Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to include only if url has a certain string?

string

  • Please log in to reply
5 replies to this topic

#1 tut

tut

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 14 March 2012 - 10:55 AM

How do I include certain php file depdending on certain url string?
  • 0

#2 Blimp

Blimp

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 199 posts
  • Location:West Sussex, England
  • Programming Language:Java, C++, C#, PHP, (Visual) Basic, Python, JavaScript, Transact-SQL

Posted 16 March 2012 - 02:43 AM

Okay, let me explain something for you so you can better understand how it will work, then I'll give you the code.

The way you'd do this is through the PHP get function. When visiting a facebook profile (For example), you will see that the url is something like this:
Beth Heindel | Facebook
This is because facebook are using the GET function in order to get the ID of the user, then use it to query their databases and dispaly the data accordingly.
In order for a GET function to work, you'll need to create a variable which catches that response. Here's an example:
$number = $_GET['num'];
printf($number);
Now, if I was my site with this like:
www.somesite.com/somefile.php?num=3
The file would print out the letter 3, because the GET function is getting the value of 'num' (which is at the end of the URL) and printing it to the page.

I hope this helps!
  • 0

#3 tut

tut

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 16 March 2012 - 05:28 PM

Hi,

thanks for your help. Need this to include menu in right language in dependency on url.

Example:

<?php 
if (strstr($url, "some_string"))
{
include('/some/file.html');
}
?>


Real example
<?php 
if (strstr($url, "en."))
{
include('http://mysite.com/menu/menu_en.html');
}
?>


But it seems it doesn t work.
  • 0

#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 18 March 2012 - 11:14 PM

Real example

<?php 
if (strstr($url, "en."))
{
include('http://mysite.com/menu/menu_en.html');
}
?>


But it seems it doesn t work.


If this was a real example, $url would (at first look) not be defined. Your job is to try to tell us:
  • How "doesn't" it work?
  • If $url does not match, then what is $url?
For example, using your code with an appropriate address:
<?php  
$url = "http://test.com/menu/foo_en.php";
if (strstr($url, "en."))  
{  
        echo "Found \"en.\" in \$url!";
} else {
        echo "Not found";
}
?>


Simulation of this code:
Ideone.com | Online PHP Interpreter & Debugging Tool

Do you want $url to be the current address for example? where is it defined? why are you looking for "en." when there may be better options?

Alexander.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#5 bbqroast

bbqroast

    Codecall Addict

  • Senior Member
  • PipPipPipPipPipPip
  • 564 posts
  • Location:/etc/passwd

Posted 13 April 2012 - 12:55 AM

I think he wants to know if the URL has "en." in it. In which case you want one of the server variables.
  • 0
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 13 April 2012 - 01:21 AM

I believe it's better to make a multilanguage page in a more generic way. Don't make different files for different languages, make a generic page that uses translations instead. You could store strings in a database table and use the corresponding string for the choosen language, you could start a session and put the choosen language code in the session variable, maybe defaulting to english but on changing language depending on user settings or flag clicking set the var to the current language code. It will also give you a better url, in my opinion. Also, it's much easier to translate a page to a new language this way, and you don't have to rebuild the file structure.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download