PHP Cannot Specify Link Destination by Absolute Path

Asked 2 years ago, Updated 2 years ago, 142 views

I am using XAMPP in Windows 10 to create a PHP site.

I created a "test" folder directly under the "htdocs" folder, and I created "firstPage.php" and "secondPage.php" in it.
I am currently running it with XAMPP in the local environment, but I wanted to upload it to the server someday (please leave the security story behind), so I tried to specify the path to which I was linked as the absolute path.
However, although firstPage was displayed correctly, it cannot be moved to secondPage using the absolute path.
You can move by using the relative path.
The source code for firstPage is as follows:

<!DOCTYPE html>
<html>

<head>
  <metacharset="UTF-8">
</head>

<body>

// This can't be moved.
<?phpecho'<a href='.__DIR__.'/secondPage.php>secondPage</a>br/>'?>

// This can be moved.
<?phpecho'<a href=secondPage.php>secondPage</a><br/>'?>

</body>

</html>

When you view the source in your browser, the link is

<a href=C:\xampp\htdocs\test/secondPage.php>secondPage</a>br/>

<a href=secondPage.php>secondPage</a><br/>

It says , but I honestly don't know how this difference affects me.
I don't think Apache is working well...

Additional information

Both links are equally blue on the browser, but if you use __DIR__, you can click on them and they are unresponsive.

<?phpecho'<a href='.__DIR__.'/secondPage.php>secondPage</a><br/>'?>

There was no change in ./secondPage.php 」 in to .\secondPage.php 」.
Also, include and require_once can be loaded using _DIR__.
For now, it seems to be a problem with links.

php html apache xampp

2022-09-30 21:35

2 Answers

Relative and absolute paths are used to describe the path of a file, but you should be aware of the difference between local environment and Internet address (=URL) starting with http or https.

__DIR__ is just a variable that represents the path in the local environment, so if you want to specify an absolute path as an Internet address through Apache, try $_SERVER['SERVER_NAME'].

<a href="<?phpecho'http://'.$_SERVER {"SERVER_NAME"}.'/secondPage.php';?>">secondPage<a>

If you are testing locally and the hostname is "localhost", then $_SERVER{"SERVER_NAME"} should also contain that hostname.

Note:
PHP:$_SERVER-Manual
PHP ($_SERVER) Server Variable List and Example


2022-09-30 21:35

PHP is not aware of Apache's Document Root.The __DIR__ is not a directory represented by Apache's Document Root path, but a directory represented by an absolute path represented by the root directory of your computer.


2022-09-30 21:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.