Jump to content

Recommended Posts

Hi,

 

Is it possible to dynamically change a variable depending on the URL of the page?

 

i.e

 

 

my code on the page is:  $CODE = "somethinghere";

 

if the page url is www.domain.com/654321.php

 

it chages it to $CODE = "654321";

 

 

I hope I've explained this correctly!

 

 

Link to comment
https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/
Share on other sites

Sure. It's called URL rewriting. What actually happens is requests for /654321.php transparently (user doesn't know) get rewritten to something like /page.php?code=654321. Then you can get your $CODE from the $_GET array.

If you have Apache and shared hosting (ie, you don't control the system) then yes, you'd use a .htaccess with mod_rewrite rules. Looks like

RewriteEngine on

# only match if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
# only match if the requested directory doesn't exist
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite 123.php to page.php?id=123 and stop processing
RewriteRule ^(\d+)\.php$ page.php?id=$1 [L]
Though the two RewriteConds probably aren't even necessary.

You would test it by... putting the stuff in place, writing code, and seeing if it works.

 

Did you put that stuff in a .htaccess file in your website directory? Is your local setup and your web host configured to allow using .htaccess files at all?

Easy way to check that second question is to put

asdf
stuff in the .htaccess and seeing what happens to your site: if your site loads then .htaccess isn't being used and you need to deal with that, and if not then remove that stuff because it is being used.

I just want to make sure that I have explained myself correctly in my original post.

 

 

My full URL's are like this:

http://mysite.com/PPP/awr99-1322.php

 

 

Within my code  I have:

$CODE = "awr99-1322"; (This then generates everything on my page to be working correctly)

 

 

I need the $CODE="---------";   to be generated every time depending on url name.

 

 

So for example,  if the url was:

http://mysite.com/PPP/awr100-1322.php

 

Then the code would end up as,  $CODE = "awr100-1322";

No,  awr100-1322.php is the page URL.

 

I want the code within the page to read the url and populate itself with the page url (minus the .php)

i.e

 $CODE = "Page URL Here"; 

At the moment, I have to manually open the page and change the $CODE to what ever the page Url is - I want this to happen dynamically

Edited by roldahayes
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.