Hello all, I'm trying to build a forum site. The code below works fine but I dont know how to catch it from the address bar. I'm having trouble finding tutorials to help me with this.

viewtopics.php

echo "<tr bgcolor='#6698FF' ><td><a href='viewcomments.php?id=".$rows['topic']."'>
".$rows['topic']."</a></td><td>".$rows['username']."</td><td>".$rows['timestamp']."</td></tr>";

Address Bar

http://localhost/testsite/viewcomments.php?id=Cowboys%20Dancehall

I'm trying to use this in code below in viewcomments.php to retrieve the address info

$id = $HTTP_GET_VARS['topic'];

I'm getting this error
"Notice: Undefined variable: HTTP_GET_VARS in C:\wamp\www\testsite\viewcomments.php"

Dani AI

Generated

Short summary: the undefined-variable error happened because HTTP_GET_VARS is the old "long" predefined array that modern PHP does not provide by default. The correct modern approaches are the superglobal $_GET or the filter extension (e.g. filter_input()), which is what fixed the problem in this thread after suggested using the modern API. (php.net)

Use a small, safe workflow: read the raw query value, query with a prepared statement, URL-encode when building links, and escape for HTML when printing. Example pattern (replace the PDO setup with your own connection):

$topic = filter_input(INPUT_GET, 'id', FILTER_UNSAFE_RAW);
if ($topic === null || $topic === false) { /* handle missing */ }

$stmt = $pdo->prepare('SELECT author, body, created_at FROM comments WHERE topic = :topic');
$stmt->execute([':topic' => $topic]);
$comments = $stmt->fetchAll();

echo '<a href="viewcomments.php?id=' . rawurlencode($topic) . '">' .
     htmlspecialchars($topic, ENT_QUOTES, 'UTF-8') . '</a>';

This uses filter_input() to access the query string, prepared statements to avoid SQL injection, rawurlencode() so spaces become %20 in the href, and htmlspecialchars() to prevent XSS when printing the topic. (php.net)

Quick tips and cautions: prefer numeric primary keys (e.g. topic_id) in URLs or generate a URL-safe slug to avoid relying on raw titles. Do not rely on FILTER_SANITIZE_STRING (it was deprecated) — use context-specific escaping (SQL via prepared statements; HTML via htmlspecialchars). If running very old PHP, you might still see register_long_arrays options, but modern code should use the superglobals and filtering shown above. (php.net)

Recommended Answers

All 4 Replies

Member Avatar for Member #120589

EEK! Which version of PHP are you using? I thought HTTP_GET_VARS was T_REX's extinct cousin.

lol.... perhaps thats why it's not working. What alternative coding should I replace it with?

if you wnat to get the id value from the address bar, try this

$id=$_GET['id']

Thank you!! Solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.