Een functie om een volledig pad te tonen die werkt zowel op localhost als op een live server
Eerst dacht ik eraan $_SERVER['DOCUMENT_ROOT'] te gebruiken maar dit pakt dus alleen maar de root. Ik schrijf via git naar een backupmap 1 niveau dieper dan public_html en met $_SERVER['DOCUMENT_ROOT'] pakken de includes dus de bestanden uit public_html en niet uit de map die 1 niveau dieper ligt, de backup map.
Toen dacht ik getcwd(); te kunnen gebruiken, dit lost namelijk wel bovenstaand probleem op maar toen kwam ik erachter dat op localhost een pad er nu ongeveer zo uitziet: C:\xampp\htdocs\admin Dit werkt dus niet voor het includen van lokale bestanden.
Wat kan ik doen zodat mijn include altijd werkt, ongeacht waar het bestand staat?
dirname(dirname(__FILE__)) in plaats van $_SERVER['DOCUMENT_ROOT'].
Voornamelijk omdat $_SERVER['DOCUMENT_ROOT'] bij scripts die als cronjob gebruikt worden een andere waarde hebben. Maar in jouw situatie zal deze ook prima passen.
Ik heb ook $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] geprobeerd maar dit is dan weer voor de url van de huidige pagina, niet waar het bestand fysiek staat. In mijn geval krijg ik dan dit: localhost/login
Het toont alle voorkomende variabelen.
Vergelijk ze.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<style>
code {color: red;}
</style>
<hr>
<p>done met xml (@php_uname)</p>
<hr>
<?php
echo "Hostname (n) : <code>" . @php_uname(n) . "</code><br />\n";
if (function_exists( 'shell_exec' )) {
echo "Hostname : <code>" . @gethostbyname(trim(`hostname`)) . "</code><br />\n";
}
else {
echo "Server IP : <code>". $_SERVER['SERVER_ADDR'] . "</code><br />\n";
}
echo "Platform (s) : <code>" . @php_uname(s) . "</code>, (r) : <code>" . @php_uname(r) . "</code>, (v) : <code>" . @php_uname(v) . "</code><br />\n";
echo "Architecture (m) : <code>" . @php_uname(m) . "</code><br />\n";
echo "Username : <code>" . get_current_user () . "</code> ( UiD: <code>" . getmyuid() . "</code>, GiD: <code>" . getmygid() . "</code> )<br />\n";
echo "Curent Path (getcwd) : <code>" . getcwd () . "</code><br />\n";
echo "--FILE-- : <code>" . __FILE__ . "</code><br />\n";
echo "dirname FILE : <code>" . dirname(__FILE__) . "</code><br />\n";
echo "basename FILE : <code>" . basename(__FILE__) . "</code><br />\n";
echo "dirname SCRIPT_NAME : <code>" . dirname($_SERVER["SCRIPT_NAME"]) . "</code><br />\n";
echo "basename SCRIPT_NAME : <code>" . basename($_SERVER["SCRIPT_NAME"]) . "</code><br />\n";
echo "<hr>";
$path_parts = pathinfo(__FILE__);
echo '$path_parts = <code>pathinfo(__FILE__)</code><br />' . "\n";
echo '$path_parts[' . "'dirname']'" . "<br />\n";
echo "dirname : <code>" . $path_parts['dirname'], "</code><br />\n";
echo "basename : <code>" . $path_parts['basename'], "</code><br />\n";
echo "extension : <code>" . $path_parts['extension'], "</code><br />\n";
echo "filename : <code>" . $path_parts['filename'], "</code><br />\n"; // since PHP 5.2.0
echo "<hr>";
echo '<p>All names have prefix <b>$_SERVER["name"]</b></p>';
echo "USER " . " : <code>" . $_SERVER["USER"] . "</code><br />\n";
echo "HOME " . " : <code>" . $_SERVER["HOME"] . "</code><br />\n";
echo "FCGI_ROLE " . " : <code>" . $_SERVER["FCGI_ROLE"] . "</code><br />\n";
echo "REDIRECT_MOD_X_SENDFILE_ENABLED" . " : <code>" . $_SERVER["REDIRECT_MOD_X_SENDFILE_ENABLED"] . "</code><br />\n";
echo "REDIRECT_HANDLER " . " : <code>" . $_SERVER["REDIRECT_HANDLER"] . "</code><br />\n";
echo "REDIRECT_STATUS " . " : <code>" . $_SERVER["REDIRECT_STATUS"] . "</code><br />\n";
echo "MOD_X_SENDFILE_ENABLED " . " : <code>" . $_SERVER["MOD_X_SENDFILE_ENABLED"] . "</code><br />\n";
echo "HTTP_HOST " . " : <code>" . $_SERVER["HTTP_HOST"] . "</code><br />\n";
echo "HTTP_USER_AGENT " . " : <code>" . $_SERVER["HTTP_USER_AGENT"] . "</code><br />\n";
echo "HTTP_ACCEPT " . " : <code>" . $_SERVER["HTTP_ACCEPT"] . "</code><br />\n";
echo "HTTP_ACCEPT_LANGUAGE " . " : <code>" . $_SERVER["HTTP_ACCEPT_LANGUAGE"] . "</code><br />\n";
echo "HTTP_ACCEPT_ENCODING " . " : <code>" . $_SERVER["HTTP_ACCEPT_ENCODING"] . "</code><br />\n";
echo "HTTP_CONNECTION " . " : <code>" . $_SERVER["HTTP_CONNECTION"] . "</code><br />\n";
echo "PATH " . " : <code>" . $_SERVER["PATH"] . "</code><br />\n";
echo "SERVER_SIGNATURE " . " : <code>" . $_SERVER["SERVER_SIGNATURE"] . "</code><br />\n";
echo "SERVER_SOFTWARE " . " : <code>" . $_SERVER["SERVER_SOFTWARE"] . "</code><br />\n";
echo "SERVER_NAME " . " : <code>" . $_SERVER["SERVER_NAME"] . "</code><br />\n";
$ip_server = $_SERVER["SERVER_ADDR"];
if (!filter_var($ip_server, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
$ip_server_echo = "is a valid IPv6 address";
} elseif (!filter_var($ip_server, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
$ip_server_echo = "is a valid IPv4 address";
}
echo "SERVER_ADDR " . " : <code>" . $_SERVER["SERVER_ADDR"] . "</code> $ip_server_echo<br />\n";
echo "SERVER_PORT " . " : <code>" . $_SERVER["SERVER_PORT"] . "</code><br />\n";
$ip_remote = $_SERVER["REMOTE_ADDR"];
if (!filter_var($ip_remote, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
$ip_remote_echo = "is a valid IPv6 address";
} elseif (!filter_var($ip_remote, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
$ip_remote_echo = "is a valid IPv4 address";
}
echo "REMOTE_ADDR " . " : <code>" . $_SERVER["REMOTE_ADDR"] . "</code> $ip_remote_echo<br />\n";
echo "DOCUMENT_ROOT " . " : <code>" . $_SERVER["DOCUMENT_ROOT"] . "</code><br />\n";
echo "SERVER_ADMIN " . " : <code>" . $_SERVER["SERVER_ADMIN"] . "</code><br />\n";
echo "SCRIPT_FILENAME " . " : <code>" . $_SERVER["SCRIPT_FILENAME"] . "</code><br />\n";
echo "REMOTE_PORT " . " : <code>" . $_SERVER["REMOTE_PORT"] . "</code><br />\n";
echo "REDIRECT_URL " . " : <code>" . $_SERVER["REDIRECT_URL"] . "</code><br />\n";
echo "GATEWAY_INTERFACE " . " : <code>" . $_SERVER["GATEWAY_INTERFACE"] . "</code><br />\n";
echo "SERVER_PROTOCOL " . " : <code>" . $_SERVER["SERVER_PROTOCOL"] . "</code><br />\n";
echo "REQUEST_METHOD " . " : <code>" . $_SERVER["REQUEST_METHOD"] . "</code><br />\n";
echo "QUERY_STRING " . " : <code>" . $_SERVER["QUERY_STRING"] . "</code><br />\n";
echo "REQUEST_URI " . " : <code>" . $_SERVER["REQUEST_URI"] . "</code><br />\n";
echo "SCRIPT_NAME " . " : <code>" . $_SERVER["SCRIPT_NAME"] . "</code><br />\n";
echo "ORIG_SCRIPT_FILENAME " . " : <code>" . $_SERVER["ORIG_SCRIPT_FILENAME"] . "</code><br />\n";
echo "ORIG_PATH_INFO " . " : <code>" . $_SERVER["ORIG_PATH_INFO"] . "</code><br />\n";
echo "ORIG_PATH_TRANSLATED " . " : <code>" . $_SERVER["ORIG_PATH_TRANSLATED"] . "</code><br />\n";
echo "ORIG_SCRIPT_NAME " . " : <code>" . $_SERVER["ORIG_SCRIPT_NAME"] . "</code><br />\n";
echo "PHP_SELF " . " : <code>" . $_SERVER["PHP_SELF"] . "</code><br />\n";
echo "REQUEST_TIME_FLOAT " . " : <code>" . $_SERVER["REQUEST_TIME_FLOAT"] . "</code><br />\n";
echo "REQUEST_TIME " . " : <code>" . $_SERVER["REQUEST_TIME"] . "</code><br />\n";
?>
code {color: red;}
</style>
<hr>
<p>done met xml (@php_uname)</p>
<hr>
<?php
echo "Hostname (n) : <code>" . @php_uname(n) . "</code><br />\n";
if (function_exists( 'shell_exec' )) {
echo "Hostname : <code>" . @gethostbyname(trim(`hostname`)) . "</code><br />\n";
}
else {
echo "Server IP : <code>". $_SERVER['SERVER_ADDR'] . "</code><br />\n";
}
echo "Platform (s) : <code>" . @php_uname(s) . "</code>, (r) : <code>" . @php_uname(r) . "</code>, (v) : <code>" . @php_uname(v) . "</code><br />\n";
echo "Architecture (m) : <code>" . @php_uname(m) . "</code><br />\n";
echo "Username : <code>" . get_current_user () . "</code> ( UiD: <code>" . getmyuid() . "</code>, GiD: <code>" . getmygid() . "</code> )<br />\n";
echo "Curent Path (getcwd) : <code>" . getcwd () . "</code><br />\n";
echo "--FILE-- : <code>" . __FILE__ . "</code><br />\n";
echo "dirname FILE : <code>" . dirname(__FILE__) . "</code><br />\n";
echo "basename FILE : <code>" . basename(__FILE__) . "</code><br />\n";
echo "dirname SCRIPT_NAME : <code>" . dirname($_SERVER["SCRIPT_NAME"]) . "</code><br />\n";
echo "basename SCRIPT_NAME : <code>" . basename($_SERVER["SCRIPT_NAME"]) . "</code><br />\n";
echo "<hr>";
$path_parts = pathinfo(__FILE__);
echo '$path_parts = <code>pathinfo(__FILE__)</code><br />' . "\n";
echo '$path_parts[' . "'dirname']'" . "<br />\n";
echo "dirname : <code>" . $path_parts['dirname'], "</code><br />\n";
echo "basename : <code>" . $path_parts['basename'], "</code><br />\n";
echo "extension : <code>" . $path_parts['extension'], "</code><br />\n";
echo "filename : <code>" . $path_parts['filename'], "</code><br />\n"; // since PHP 5.2.0
echo "<hr>";
echo '<p>All names have prefix <b>$_SERVER["name"]</b></p>';
echo "USER " . " : <code>" . $_SERVER["USER"] . "</code><br />\n";
echo "HOME " . " : <code>" . $_SERVER["HOME"] . "</code><br />\n";
echo "FCGI_ROLE " . " : <code>" . $_SERVER["FCGI_ROLE"] . "</code><br />\n";
echo "REDIRECT_MOD_X_SENDFILE_ENABLED" . " : <code>" . $_SERVER["REDIRECT_MOD_X_SENDFILE_ENABLED"] . "</code><br />\n";
echo "REDIRECT_HANDLER " . " : <code>" . $_SERVER["REDIRECT_HANDLER"] . "</code><br />\n";
echo "REDIRECT_STATUS " . " : <code>" . $_SERVER["REDIRECT_STATUS"] . "</code><br />\n";
echo "MOD_X_SENDFILE_ENABLED " . " : <code>" . $_SERVER["MOD_X_SENDFILE_ENABLED"] . "</code><br />\n";
echo "HTTP_HOST " . " : <code>" . $_SERVER["HTTP_HOST"] . "</code><br />\n";
echo "HTTP_USER_AGENT " . " : <code>" . $_SERVER["HTTP_USER_AGENT"] . "</code><br />\n";
echo "HTTP_ACCEPT " . " : <code>" . $_SERVER["HTTP_ACCEPT"] . "</code><br />\n";
echo "HTTP_ACCEPT_LANGUAGE " . " : <code>" . $_SERVER["HTTP_ACCEPT_LANGUAGE"] . "</code><br />\n";
echo "HTTP_ACCEPT_ENCODING " . " : <code>" . $_SERVER["HTTP_ACCEPT_ENCODING"] . "</code><br />\n";
echo "HTTP_CONNECTION " . " : <code>" . $_SERVER["HTTP_CONNECTION"] . "</code><br />\n";
echo "PATH " . " : <code>" . $_SERVER["PATH"] . "</code><br />\n";
echo "SERVER_SIGNATURE " . " : <code>" . $_SERVER["SERVER_SIGNATURE"] . "</code><br />\n";
echo "SERVER_SOFTWARE " . " : <code>" . $_SERVER["SERVER_SOFTWARE"] . "</code><br />\n";
echo "SERVER_NAME " . " : <code>" . $_SERVER["SERVER_NAME"] . "</code><br />\n";
$ip_server = $_SERVER["SERVER_ADDR"];
if (!filter_var($ip_server, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
$ip_server_echo = "is a valid IPv6 address";
} elseif (!filter_var($ip_server, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
$ip_server_echo = "is a valid IPv4 address";
}
echo "SERVER_ADDR " . " : <code>" . $_SERVER["SERVER_ADDR"] . "</code> $ip_server_echo<br />\n";
echo "SERVER_PORT " . " : <code>" . $_SERVER["SERVER_PORT"] . "</code><br />\n";
$ip_remote = $_SERVER["REMOTE_ADDR"];
if (!filter_var($ip_remote, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
$ip_remote_echo = "is a valid IPv6 address";
} elseif (!filter_var($ip_remote, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
$ip_remote_echo = "is a valid IPv4 address";
}
echo "REMOTE_ADDR " . " : <code>" . $_SERVER["REMOTE_ADDR"] . "</code> $ip_remote_echo<br />\n";
echo "DOCUMENT_ROOT " . " : <code>" . $_SERVER["DOCUMENT_ROOT"] . "</code><br />\n";
echo "SERVER_ADMIN " . " : <code>" . $_SERVER["SERVER_ADMIN"] . "</code><br />\n";
echo "SCRIPT_FILENAME " . " : <code>" . $_SERVER["SCRIPT_FILENAME"] . "</code><br />\n";
echo "REMOTE_PORT " . " : <code>" . $_SERVER["REMOTE_PORT"] . "</code><br />\n";
echo "REDIRECT_URL " . " : <code>" . $_SERVER["REDIRECT_URL"] . "</code><br />\n";
echo "GATEWAY_INTERFACE " . " : <code>" . $_SERVER["GATEWAY_INTERFACE"] . "</code><br />\n";
echo "SERVER_PROTOCOL " . " : <code>" . $_SERVER["SERVER_PROTOCOL"] . "</code><br />\n";
echo "REQUEST_METHOD " . " : <code>" . $_SERVER["REQUEST_METHOD"] . "</code><br />\n";
echo "QUERY_STRING " . " : <code>" . $_SERVER["QUERY_STRING"] . "</code><br />\n";
echo "REQUEST_URI " . " : <code>" . $_SERVER["REQUEST_URI"] . "</code><br />\n";
echo "SCRIPT_NAME " . " : <code>" . $_SERVER["SCRIPT_NAME"] . "</code><br />\n";
echo "ORIG_SCRIPT_FILENAME " . " : <code>" . $_SERVER["ORIG_SCRIPT_FILENAME"] . "</code><br />\n";
echo "ORIG_PATH_INFO " . " : <code>" . $_SERVER["ORIG_PATH_INFO"] . "</code><br />\n";
echo "ORIG_PATH_TRANSLATED " . " : <code>" . $_SERVER["ORIG_PATH_TRANSLATED"] . "</code><br />\n";
echo "ORIG_SCRIPT_NAME " . " : <code>" . $_SERVER["ORIG_SCRIPT_NAME"] . "</code><br />\n";
echo "PHP_SELF " . " : <code>" . $_SERVER["PHP_SELF"] . "</code><br />\n";
echo "REQUEST_TIME_FLOAT " . " : <code>" . $_SERVER["REQUEST_TIME_FLOAT"] . "</code><br />\n";
echo "REQUEST_TIME " . " : <code>" . $_SERVER["REQUEST_TIME"] . "</code><br />\n";
?>
er is een url: http://domeinnaam.nl/some/path/to/script
en er is een plek op de server waar de files werkelijk staan.
Bijvoorbeeld
/var/www/html/domein/subdomein/some/path
of d:\data\sites\domein\some\path als je windows gebruikt op je server.
Vaak zit er een deel overlap in de mappenstruktuur (some/path) maar dat hoeft niet.
Voor includes en cronjobs heb je het lokale path nodig. Voor aanroepen via de browser moet je een url gebruiken. Behalve als je een html-pagina rechtstreeks van je disk kan laden, maar dan werkt doorgaans PHP niet,omdat je buiten je webserver om werkt in dat geval.
Om de huidige url te pakken:
https://www.phphulp.nl/php/forum/topic/een-functie-om-een-volledig-pad-te-tonen-die-werkt-zowel-op-localhost-als-op-een-live-server/104241/last/
Het zou zo maar kunnen dat we hier kijken naar het resultaat van een script dat luistert naar de naam
/var/www/phphulp/forum/forum.php
Maar dat is voor ons als bezoekers niet interessant.
Aan de andere kant kan je in je script weer weinig met https://www.phphulp.nl/php/forum/topic/ om iets te includen aangezien je dan gewoon een url aanroept en je dan niet beschikt over de variabelen etc die je in je huidige script gebruikt
@adoptive solutions , dit script zal nog wel is van pas komen denk ik.
Gewijzigd op 26/01/2022 13:44:13 door Snelle Jaap
Zorg ervoor dat alle belangrijke bestanden die niet door de browser opgevraagd worden hoger in de root worden opgeslagen, en dus buiten /public_html.
Op die manier kan je meerdere versies opslaan, en via een configuratie bepalen welke versie je wilt.
Zo kan je ook heel makkelijk updaten zonder veel downtime.