Skip to content

Commit

Permalink
Parameterize queries to prevent against SQL injection
Browse files Browse the repository at this point in the history
  • Loading branch information
copperwall committed Mar 19, 2015
1 parent 0c8b777 commit 2203d4c
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@
$app->get('/feed', function() use ($app) {
$db = TwidditDB::db();
$username = $_COOKIE['user'];
$query = "select redditor
from followingRedditors
where '$username' = userName";
$result = $db->query($query);
$query = <<<EOT
SELECT `redditor`
FROM `followingRedditors`
WHERE `userName` = :username
EOT;

$statement = $db->prepare($query);
$statement->bindParam(':username', $username);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);

$users = [];
foreach ($result as $row) {
$users[] = $row['redditor'];
Expand All @@ -50,18 +57,26 @@

echo json_encode($comments);
});

$app->get('/subreddits', function() use ($app) {
$db = TwidditDB::db();
$username = $_COOKIE['user'];
$query = "select subreddit
from followingSubreddit
where '$username' = userName";
$result = $db->query($query);
$subreddit = [];
foreach ($result as $row) {
$subreddit[] = $row['subreddit'];
$query = <<<EOT
SELECT `subreddit`
FROM `followingSubreddit`
WHERE `userName` = :username
EOT;

$statement = $db->prepare($query);
$statement->bindParam(':username', $username);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);

$subreddits = [];
foreach ($results as $row) {
$subreddits[] = $row['subreddit'];
}
$data = Reddit::getSubredditPosts($subreddit);
$data = Reddit::getSubredditPosts($subreddits);

echo json_encode($data);
});
Expand Down Expand Up @@ -107,7 +122,6 @@
}
});


$app->post('/signup', function() use ($app) {
$db = TwidditDB::db();
$username = $app->request->post('username');
Expand Down

0 comments on commit 2203d4c

Please sign in to comment.