Avatar of prinLeA
prinLeA
 asked on

You don't have permission to access /phpdatabasefive/getUsefulness/post.php> on this server

Hello,

Need some help this is really a simple issue not really a problem but I'm a beginner there must be something in the setting that I'm missing or I don't know yet. Anyhow I got this from a book, I'm trying to learn PHP. Here is the code:

There are three files
1. post.php
2. list-posts.php
3. single-post.php

On post.php
<?php
class Posts {
	public $db = '';
	public function __construct() {
		$this->db = new PDO("mysql:host=localhost;dbname=testwopost", "root", "");
		$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$this->index();
	}
	public function index() {
	$id = 0;
	$posts = array();
	$template = '';
	if (!empty($_GET['id'])) {
	$id = $_GET['id'];
	}
	try {
		if (!empty($id)) {
		$query = $this->db->prepare("SELECT * FROM posts WHERE id = ?");
		$params = array($id);
		$template = 'single-post.php';
		} else {
		$query = $this->db->prepare("SELECT * FROM posts");
		$params = array();
		$template = 'list-posts.php';
		}
		$query->execute($params);
		for ($i = 0; $row = $query->fetch(); $i++) {
			$posts[] = array('id' => $row['id'], 'content' => $row['content']);
			}
		} catch (PDOException $e) {
			echo $e->getMessage();
		}
		$query->closeCursor();
		$db = null;
		require_once($template);
		}
	}
	$posts = new Posts();
	?>

Open in new window


On list-posts.php
<h1>List of Blog Posts</h1>
<?php foreach ($posts as $post): ?>
	<h3>Post #<?php echo htmlspecialchars($post['id']); ?></h3>
	<?php echo htmlspecialchars($post['content']); ?>
	<a href="http://localhost/phpDatabaseFive/getUsefulness/post.php?id=<?php echo htmlspecialchars($post['id']); ?>">Read More</a> <hr />
<?php endforeach; ?>

Open in new window


On single-post.php
<?php foreach($posts as $post): ?>
<h1>Post #<?php echo htmlspecialchars($post['id']); ?></h1>
<hr />
<?php echo htmlspecialchars($post['content']); ?>
<a href="http://localhost/phpDatabaseFive/getUsefulness/post.php>"> Backto Post List</a>
<?php endforeach; ?>

Open in new window


Everything seems working fine but on single-post.php when you click Backto Post List it will say:
Forbidden

You don't have permission to access /phpdatabasefive/getUsefulness/post.php> on this server.

Thanks for the help
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Systech Admin

it seems that the database is not having permission to the user kindly check the database if the mentioned user is having permissions on the database
Ray Paseur

What is the server operating system?  Is the header a "403 Forbidden" error?

If you're new to PHP and want to learn the language, this article can lead you to dependable learning resources, and more importantly, can help you avoid the many dangerous, wrong, and obsolete examples of PHP code that litter the internet.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
prinLeA

ASKER
Hello,

To sir Gaurav Singh yes sir the user has the full privilege I'm using root. To sir Ray Paseur the server OS is actually my local machine and I'm using Windows 8.1. It's a WAMP sir. The error says:

Forbidden

You don't have permission to access /phpdatabasefive/getUsefulness/post.php> on this server. and below it I can read this: Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80

Thanks for the help I really appreciate.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
prinLeA

ASKER
Excellent! Very nice Expert-exchange is the best I hope it dominates the net where everyone find solution fast instead of wasting time with search engine
Ray Paseur

Thanks for the points and thanks for using E-E, ~Ray