- Joined
- Aug 21, 2023
- Messages
- 40
- Reaction score
- 0
Hello everyone,
I have a problem with the final chapter of the book "Learn PHP, MySQL and JavaScript". The thing is that when I want to load the index.php locally using AMPPS it doesn't work at all. It does not show me even the index of that chapter. I did not have any problems with the earlier chapters. The code is big but if anybody knows if there is a mistake let me know.
First is the functions.php :
Than the header.php :
Than the setup.php :
And the index.php :
Till now I should be able to see how the site looks. Of corse there is a styles.css but it would be too much from me in this post.
Thanks.
I have a problem with the final chapter of the book "Learn PHP, MySQL and JavaScript". The thing is that when I want to load the index.php locally using AMPPS it doesn't work at all. It does not show me even the index of that chapter. I did not have any problems with the earlier chapters. The code is big but if anybody knows if there is a mistake let me know.
First is the functions.php :
PHP:
<?php // Example 01: functions.php
$host = 'localhost'; // Change as necessary
$data = 'robinsnest'; // Change as necessary
$user = 'robinsnest'; // Change as necessary
$pass = 'password'; // Change as necessary
$chrs = 'utf8mb4';
$attr = "mysql:host=$host;dbname=$data;charset=$chrs";
$opts =
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try
{
$pdo = new PDO($attr, $user, $pass, $opts);
}
catch (PDOException $e)
{
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
function createTable($name, $query)
{
queryMysql("CREATE TABLE IF NOT EXISTS $name($query)");
echo "Table '$name' created or already exists.<br>";
}
function queryMysql($query)
{
global $pdo;
return $pdo->query($query);
}
function destroySession()
{
$_SESSION=array();
if (session_id() != "" || isset($_COOKIE[session_name()]))
setcookie(session_name(), '', time()-2592000, '/');
session_destroy();
}
function sanitizeString($var)
{
global $pdo;
$var = strip_tags($var);
$var = htmlentities($var);
$var = stripslashes($var);
$result = $pdo->quote($var); // This adds single quotes
return str_replace("'", "", $result); // So now remove them
}
function showProfile($user)
{
global $pdo;
if (file_exists("$user.jpg"))
echo "<img src='$user.jpg' style='float:left;'>";
$result = $pdo->query("SELECT * FROM profiles WHERE user='$user'");
while ($row = $result->fetch())
{
die(stripslashes($row['text']) . "<br style='clear:left;'><br>");
}
echo "<p>Nothing to see here, yet</p><br>";
}
?>
Than the header.php :
PHP:
<?php // Example 02: header.php
session_start();
echo <<<_INIT
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' href='jquery.mobile-1.4.5.min.css'>
<link rel='stylesheet' href='styles.css' type='text/css'>
<script src='javascript.js'></script>
<script src='jquery-2.2.4.min.js'></script>
<script src='jquery.mobile-1.4.5.min.js'></script>
_INIT;
require_once 'functions.php';
$userstr = 'Welcome Guest';
$randstr = substr(md5(rand()), 0, 7);
if (isset($_SESSION['user']))
{
$user = $_SESSION['user'];
$loggedin = TRUE;
$userstr = "Logged in as: $user";
}
else $loggedin = FALSE;
echo <<<_MAIN
<title>Robin's Nest: $userstr</title>
</head>
<body>
<div data-role='page'>
<div data-role='header'>
<div id='logo' class='center'>R<img id='robin' src='robin.gif'>bin's Nest</div>
<div class='username'>$userstr</div>
</div>
<div data-role='content'>
_MAIN;
if ($loggedin)
{
echo <<<_LOGGEDIN
<div class='center'>
<a data-role='button' data-inline='true' data-icon='home'
data-transition="slide" href='members.php?view=$user&r=$randstr'>Home</a>
<a data-role='button' data-inline='true' data-icon='user'
data-transition="slide" href='members.php?r=$randstr'>Members</a>
<a data-role='button' data-inline='true' data-icon='heart'
data-transition="slide" href='friends.php?r=$randstr'>Friends</a><br>
<a data-role='button' data-inline='true' data-icon='mail'
data-transition="slide" href='messages.php?r=$randstr'>Messages</a>
<a data-role='button' data-inline='true' data-icon='edit'
data-transition="slide" href='profile.php?r=$randstr'>Edit Profile</a>
<a data-role='button' data-inline='true' data-icon='action'
data-transition="slide" href='logout.php?r=$randstr'>Log out</a>
</div>
_LOGGEDIN;
}
else
{
echo <<<_GUEST
<div class='center'>
<a data-role='button' data-inline='true' data-icon='home'
data-transition='slide' href='index.php?r=$randstr''>Home</a>
<a data-role='button' data-inline='true' data-icon='plus'
data-transition="slide" href='signup.php?r=$randstr''>Sign Up</a>
<a data-role='button' data-inline='true' data-icon='check'
data-transition="slide" href='login.php?r=$randstr''>Log In</a>
</div>
<p class='info'>(You must be logged in to use this app)</p>
_GUEST;
}
?>
Than the setup.php :
PHP:
<!DOCTYPE html> <!-- Example 03: setup.php -->
<html>
<head>
<title>Setting up database</title>
</head>
<body>
<h3>Setting up...</h3>
<?php
require_once 'functions.php';
createTable('members',
'user VARCHAR(16),
pass VARCHAR(16),
INDEX(user(6))');
createTable('messages',
'id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
auth VARCHAR(16),
recip VARCHAR(16),
pm CHAR(1),
time INT UNSIGNED,
message VARCHAR(4096),
INDEX(auth(6)),
INDEX(recip(6))');
createTable('friends',
'user VARCHAR(16),
friend VARCHAR(16),
INDEX(user(6)),
INDEX(friend(6))');
createTable('profiles',
'user VARCHAR(16),
text VARCHAR(4096),
INDEX(user(6))');
?>
<br>...done.
</body>
</html>
And the index.php :
PHP:
<?php // Example 04: index.php
session_start();
require_once 'header.php';
echo "<div class='center'>Welcome to Robin's Nest,";
if ($loggedin) echo " $user, you are logged in";
else echo ' please sign up or log in';
echo <<<_END
</div><br>
</div>
<div data-role="footer">
<h4>Web App from <i><a href='https://github.com/RobinNixon/lpmj6'
target='_blank'>Learning PHP MySQL & JavaScript</a></i></h4>
</div>
</body>
</html>
_END;
?>
Till now I should be able to see how the site looks. Of corse there is a styles.css but it would be too much from me in this post.
Thanks.