PHP script problemen
Knip en plak de code voor .htaccess van bovenstaande pagina, of als je een webserver hebt die geen .htaccess ondersteunt zorg dan op een of andere manier dat alle requests worden doorgestuurd naar /index.php
.htaccess
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Enable rewriting.
RewriteEngine on
# Optional: do not allow perusal of directories.
Options -Indexes
# Optional: explicitly enable per-directory rewrites in the .htaccess context.
Options +FollowSymLinks
# Required when not in the webroot. Always use a trailing slash.
RewriteBase /
# To be able to access existing directories and files (standalone scripts).
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Redirect everything else to index.php.
# Add QSA to ensure that querystring variables are registered as such.
RewriteRule . index.php [L,QSA]
RewriteEngine on
# Optional: do not allow perusal of directories.
Options -Indexes
# Optional: explicitly enable per-directory rewrites in the .htaccess context.
Options +FollowSymLinks
# Required when not in the webroot. Always use a trailing slash.
RewriteBase /
# To be able to access existing directories and files (standalone scripts).
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Redirect everything else to index.php.
# Add QSA to ensure that querystring variables are registered as such.
RewriteRule . index.php [L,QSA]
En dan de index.php, met de vereenvoudigde variant voor het berekenen van $path. Creatie van databasetabel + inhoud zit in code. Hier is alles bij elkaar geflanst en heel simpel gehouden. Het gaat hier per slot van rekening over het uitleggen van een concept.
index.php
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
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
<?php
// debugging mode - should only be enabled on a development environment
error_reporting(E_ALL);
ini_set('display_startup_errors', true);
ini_set('display_errors', 'stdout');
// first, create a table...
/*
CREATE TABLE content (
cnt_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
cnt_path VARCHAR(255) NOT NULL UNIQUE,
cnt_title VARCHAR(255) NOT NULL,
cnt_content LONGTEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
// ... and some sample data
INSERT INTO content (cnt_path, cnt_title, cnt_content) VALUES
('', 'Welcome', '<p>This is the homepage!</p>'),
('whatever', 'Whatever!', '<p>This is the whatever page!</p>'),
('one/two/five', 'Woohoo!', '<p>Welcome to the one two <strike>three</strike> five page!</p>');
*/
// the next bit of code handles setting the $path variable which contains the application path that was originally called
// Read REQUEST_URI, suppress errors (gave E_WARNING prior to PHP 5.3.3).
$uriData = @parse_url($_SERVER['REQUEST_URI']);
$path = '';
if ($uriData === false) {
// Do something? For example set a default path
} else {
if (isset($uriData['path'])) {
$path = trim(substr($uriData['path'], strlen(dirname($_SERVER['SCRIPT_NAME']))), '/');
}
}
// some default content in case content could not be found
$content = array(
'title' => 'Four Oh Four',
'content' => '<p>Page not found :(</p>',
);
// connect to database
// $db = new mysqli('<host>', '<user>', '<password>', '<database>');
if ($db->connect_errno > 0) {
die('[error] failed to connect to database');
}
// set appropriate character encoding
$db->set_charset('utf8');
// try tro fetch data for the selected path
$res = $db->query(
"SELECT cnt_title AS title, cnt_content AS content
FROM content
WHERE cnt_path = '".$db->real_escape_string($path)."'"
);
if ($res->num_rows == 0) {
// set HTTP 404 headers because no content was found
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
} else {
// overwrite content
$content = $res->fetch_assoc();
}
$res->free();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $content['title']; ?> - Content Test</title>
</head>
<body>
<?php
// note that this list can be generated from the database too
// I will leave that as an exercise for the reader :p
?>
<ul>
<li><a href="/">to the homepage</a></li>
<li><a href="/whatever">whatever</a></li>
<li><a href="/one/two/five">one two five</a></li>
<li><a href="/nonexistent">nonexistent</a></li>
</ul>
<h1><?php echo $content['title']; ?></h1>
<?php echo $content['content']; ?>
</body>
</html>
// debugging mode - should only be enabled on a development environment
error_reporting(E_ALL);
ini_set('display_startup_errors', true);
ini_set('display_errors', 'stdout');
// first, create a table...
/*
CREATE TABLE content (
cnt_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
cnt_path VARCHAR(255) NOT NULL UNIQUE,
cnt_title VARCHAR(255) NOT NULL,
cnt_content LONGTEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
// ... and some sample data
INSERT INTO content (cnt_path, cnt_title, cnt_content) VALUES
('', 'Welcome', '<p>This is the homepage!</p>'),
('whatever', 'Whatever!', '<p>This is the whatever page!</p>'),
('one/two/five', 'Woohoo!', '<p>Welcome to the one two <strike>three</strike> five page!</p>');
*/
// the next bit of code handles setting the $path variable which contains the application path that was originally called
// Read REQUEST_URI, suppress errors (gave E_WARNING prior to PHP 5.3.3).
$uriData = @parse_url($_SERVER['REQUEST_URI']);
$path = '';
if ($uriData === false) {
// Do something? For example set a default path
} else {
if (isset($uriData['path'])) {
$path = trim(substr($uriData['path'], strlen(dirname($_SERVER['SCRIPT_NAME']))), '/');
}
}
// some default content in case content could not be found
$content = array(
'title' => 'Four Oh Four',
'content' => '<p>Page not found :(</p>',
);
// connect to database
// $db = new mysqli('<host>', '<user>', '<password>', '<database>');
if ($db->connect_errno > 0) {
die('[error] failed to connect to database');
}
// set appropriate character encoding
$db->set_charset('utf8');
// try tro fetch data for the selected path
$res = $db->query(
"SELECT cnt_title AS title, cnt_content AS content
FROM content
WHERE cnt_path = '".$db->real_escape_string($path)."'"
);
if ($res->num_rows == 0) {
// set HTTP 404 headers because no content was found
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
} else {
// overwrite content
$content = $res->fetch_assoc();
}
$res->free();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $content['title']; ?> - Content Test</title>
</head>
<body>
<?php
// note that this list can be generated from the database too
// I will leave that as an exercise for the reader :p
?>
<ul>
<li><a href="/">to the homepage</a></li>
<li><a href="/whatever">whatever</a></li>
<li><a href="/one/two/five">one two five</a></li>
<li><a href="/nonexistent">nonexistent</a></li>
</ul>
<h1><?php echo $content['title']; ?></h1>
<?php echo $content['content']; ?>
</body>
</html>
Succes ermee.
Gewijzigd op 27/11/2019 23:02:06 door Thomas van den Heuvel