4 Replies - 263 Views - Last Post: 21 January 2012 - 09:23 PM

Topic Sponsor:

#1 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 36
  • View blog
  • Posts: 599
  • Joined: 15-December 09

Links in PHP 'how do they work?'

Posted 21 January 2012 - 07:30 PM

I know how plain links work, but I've never really got any good info on how they work so they can be placed without a whole lot of trouble when re-editing code.

There's the different links.

http://www.mydomain....ctory/index.php

directory/index.php

/directory/index.php

$_SERVER['PHP_SELF']

Then there are also these globals:
'SCRIPT_NAME'
'REQUEST_URI'
'PATH_INFO'

When are these globals good to use.

Say for instance I have some links in a script and I move the script into an include. If the links are formatted like /directory/index.html the links will suddenly not work.

Is This A Good Question/Topic? 0
  • +

Replies To: Links in PHP 'how do they work?'

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7517
  • View blog
  • Posts: 28,881
  • Joined: 27-December 08

Re: Links in PHP 'how do they work?'

Posted 21 January 2012 - 08:55 PM

Avoid using the $_SERVER superglobal here, as you are opening yourself up to cross-site scripting attacks. You can set up your directory structures to use relative links. If you want to access a child directory, use /child. To access a file in the parent directory, you can do ../file_in_parent.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1940
  • View blog
  • Posts: 7,294
  • Joined: 08-August 08

Re: Links in PHP 'how do they work?'

Posted 21 January 2012 - 09:13 PM

Quote

Links in PHP 'how do they work?'

They don't. PHP doesn't do anything with links because to PHP links are just text, like all HTML. You can use PHP to build a link and then send it to the browser. When a link you've built doesn't work look at the browser's source code to see if you built it correctly.
Was This Post Helpful? 1
  • +
  • -

#4 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 36
  • View blog
  • Posts: 599
  • Joined: 15-December 09

Re: Links in PHP 'how do they work?'

Posted 21 January 2012 - 09:18 PM

View Postmacosxnerd101, on 21 January 2012 - 09:55 PM, said:

Avoid using the $_SERVER superglobal here, as you are opening yourself up to cross-site scripting attacks.


That's good to know.

View PostCTphpnwb, on 21 January 2012 - 10:13 PM, said:

Quote

Links in PHP 'how do they work?'

They don't. PHP doesn't do anything with links because to PHP links are just text, like all HTML. You can use PHP to build a link and then send it to the browser. When a link you've built doesn't work look at the browser's source code to see if you built it correctly.


I was hoping to figure out a way to use links so they can be portable between installations, and can be moved to an include without breaking.

Should I really just read the code and change them when they are moved?

This post has been edited by hiddenghost: 21 January 2012 - 09:18 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7517
  • View blog
  • Posts: 28,881
  • Joined: 27-December 08

Re: Links in PHP 'how do they work?'

Posted 21 January 2012 - 09:23 PM

Quote

I was hoping to figure out a way to use links so they can be portable between installations, and can be moved to an include without breaking.

You should use relative links, which doesn't involve any PHP- it's all HTML. If you are familiar with Unix directory structuring, set your directory structure up to take advantage of that.

This isn't a PHP question really, so I'm going to move this to Web Development for better discussion. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1