Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible errata, page 512 #5

Open
simonmackie opened this issue Jun 8, 2023 · 0 comments
Open

Possible errata, page 512 #5

simonmackie opened this issue Jun 8, 2023 · 0 comments

Comments

@simonmackie
Copy link

PDF Version
First of all, thank you Tom Butler for making this awesome book, I am enjoying it so much.
I think I found some little bugs I would like to share.
On page 512 (Chapter 11 cookies and sessions. Mission accomplished? section)
public function editSubmit($id = null) {
$author = $this->authentication->getUser();
// Checking the $id VARIABLE DON'T STOP ME FROM EDITING //OTHER'S PEOPLE JOKE since id comes from the hidden input tag
// so even $id parameter addition is not necessary
if (isset($id)) {
$joke = $this->jokesTable->find('id', $id)[0] ?? null;
if ($joke['authorId'] != $author['id']) {
// This return causes some errors since the Entry point is expecting a // title and template.
return;
}
}
$joke = $_POST['joke'];
$joke['jokedate'] = new \DateTime();
$joke['authorId'] = $author['id']; $this->jokesTable->save($joke); header('location: /joke/list');
}
My suggestion would be something like the following:
public function deleteSubmit(){
if($this->checkAuthorJokeConsistency($_POST['id'])){
$this->jokesTable->delete('id', $_POST['id']);
}
header('Location: /joke/list');
}
public function editSubmit(){
$author = $this->authentication->getUser();
if($this->checkAuthorJokeConsistency( $_POST['joke']['id'])){
$joke = $_POST['joke'];
$joke['jokedate'] = new \DateTime();
$joke['authorid'] = $author['id'];
$this->jokesTable->save($joke);
}
header('Location: /joke/list');
}
// I created this function to try to avoid the copy paste.
private function checkAuthorJokeConsistency($checkVar){
$author = $this->authentication->getUser();
$joke = $this->jokesTable->find('id', $checkVar)[0] ?? null;
if($joke['authorid'] != $author['id']){
return false;
}
else{
return true;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant