PHP passing variable id through href

PHP passing variable id through href

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Securely Passing Variable IDs in PHP Href Attributes

The code snippet you provided demonstrates a classic scenario in server-side templating: dynamically generating navigation links based on database records. Specifically, you are iterating through a collection of subjects and attempting to route users to approve.php with the current item's ID appended as a query parameter. While the intent is straightforward, the implementation contains a critical security vulnerability known as Cross-Site Scripting (XSS).

In your example, the line <a href='approve.php?id=<?php echo $sub->id; ?>' ...> concatenates user data directly into an HTML attribute without sanitization. Although IDs are often integers, relying on this assumption is a fragile pattern. If the id column in your database allows for non-integer values (which happens frequently with UUIDs or auto-incremented strings), an attacker could inject malicious JavaScript. Even if the ID is numeric, the lack of explicit context separation makes the code harder to read and maintain compared to safer alternatives.

The standard solution involves ensuring that the data is treated strictly as data, not as executable content. In modern PHP development, particularly within the Laravel ecosystem, the preferred method for handling this is using sprintf or letting the templating engine handle escaping automatically if you are using a view layer like Blade. However, since your snippet appears to be raw PHP mixed with HTML, we must address the concatenation manually.

You can refactor the link generation to use htmlspecialchars to escape output, though for numeric IDs, sprintf is often more semantically correct when dealing with attributes. Here is how you might adjust the logic:

<td>
    <?php echo htmlspecialchars($sub->subject); ?>
</td>
<td>
    <a href="approve.php?id=<?php echo (int)$sub->id; ?>" role="button" class="btn">Add Topic</a>
</td>

By casting $sub->id to an integer with (int), you ensure that any attempt to inject quotes or script tags fails, as the value is forced into a numeric context. Alternatively, using sprintf('approve.php?id=%d', $sub->id) achieves the same result with clearer intent regarding type coercion.

If you are building this application within Laravel, it is highly advisable to move away from raw PHP logic inside your HTML files. Instead, leverage Blade templates. In a Blade file, you would define the data in your controller and pass it to the view. The templating engine automatically escapes variables by default:

@foreach($subjects as $sub)
    <td>{{ $sub->subject }}</td>
    <td><a href="{{ route('approve', ['id' => $sub->id]) }}" class="btn">Add Topic</a></td>
@endforeach

Using the route() helper is the robust, Laravel-native approach. It generates a valid URL that includes the ID as a query string while protecting against XSS and ensuring consistent URL formatting. This abstraction also handles HTTP verbs correctly if you decide to switch from GET parameters to route actions later.

When choosing between passing an ID via query string (?id=5) or using a named route, consider your application's architecture. Query strings are useful for simple stateless interactions where the user needs to see the context of the request in their browser history. However, if you are building a RESTful API or a complex SPA, passing the ID in the URL structure (e.g., /approve/5) is often preferred for SEO and caching strategies.

Regardless of the syntax chosen, the core principle remains: never trust that data from the database will be safe to echo directly into HTML attributes without validation or explicit casting. Always assume external input or stored data could be manipulated. By enforcing type safety with (int) or utilizing a framework's routing helpers, you eliminate the risk of script injection and produce code that is easier for other developers to audit and extend.

Our network