Morning everyone --

Can anyone help me with what is probably very simple?

I have a page where I want to get a variable from the URL and display it... so if my url is

I want to get that "hi" out of there and print it out to the page.

Thanks in advance!

Dani AI

Generated

A short practical summary and a few modern, safe examples to complement the replies from , and .

The part after the question mark is the query string. It can be read server‑side (PHP, etc.) or client‑side (JavaScript). For quick client‑side access, modern browsers expose URLSearchParams; insert values as plain text to avoid XSS:

// modern browsers
const params = new URLSearchParams(window.location.search);
const value = params.get('variable'); // "hi" or null
if (value !== null) {
  document.getElementById('output').textContent = value;
}

Fallback for older environments (simple extractor):

function getQueryParam(name, url) {
  if (!url) url = window.location.href;
  name = name.replace(/[[]]/g, "\\$&");
  const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
  const results = regex.exec(url);
  if (!results) return null;
  if (!results[2]) return '';
  return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

Server side: as pointed out, query values arrive to the application. Treat them as untrusted. Example showing safe output and simple URL validation (modern PHP):

$val = isset($_GET['variable']) ? $_GET['variable'] : '';
echo htmlspecialchars($val, ENT_QUOTES, 'UTF-8');

If building an anchor from a variable (see ), make sure the href contains a scheme; otherwise browsers treat it as a relative path and append it to the site. A quick guard: if the value does not start with http:// or https://, prepend one or validate with filter_var(..., FILTER_VALIDATE_URL).

Troubleshooting notes: missing "?" means no query string; URL encoding turns spaces into + or %20; server rewrites can hide query strings from error pages; and always escape or use textContent on the client to prevent XSS. For a 404 page shown client‑side, use location.href, location.pathname or location.search to display the requested address.

Recommended Answers

All 13 Replies

Nevermind - I got it.

how?

please tell

It depends. It can be done in Javascript or using a serverside language.

Which would you rather do?

For reference, that part of the URL is called the 'query string'.

Server side langauges often have simplified interfaces to access the 'variables' in a query string. Javascript has no such interface; but it is possible to get reference to the entire URL of a page (address + query string), and to split that down into its component parts.

An example is here:

(That's a somewhat wasteful procedure if you want to repeatadly get at the different variables in a query string; but its not too bad if you have alot of potential variables and are only interested in a few of them)

Um, the java script lokos hard, could you please tell me how i would retrive the variable text, just the text, onto a page using php? or another server side language

With PHP you simple use the
$_GET;

Variable. For example if you had a link

Your PHP would be:

<?php
 
$id = $_GET['id'];
 
echo $id;
 
?>

this would output 12

cool, thanks

You can do it only with php.

You can do it only with php.

You can do it with any serverside language, and any client side script should be able to get the query string indirectly from the page URL.

PHP does it through Zend, which does it through C++; and it's delivered into an application's environment by the server software; so clearly, you can access it using other means than only PHP.

I'm sure you meant something different; and I don't mean to bite your head off; but there's a full wealth of ways in which you can access that data, PHP is one way to do it.

Hi,

I have a website say www.test.com.
How do i give the variables linked URL to external website in PHP.

For instance, i have a variable $xyz in php code. $xyz indicates a url say www.yahoo.com, then it opens the page as www.xyz.com/www.yahoo.com

My code is as below:

<a href="$xyz">test</a>

Hi,

I have a website say www.test.com.
How do i give the variables linked URL to external website in PHP.

For instance, i have a variable $xyz in php code. $xyz indicates a url say www.yahoo.com, then it opens the page as www.xyz.com/www.yahoo.com

My code is as below:

<a href="{$xyz}">test</a>

Hi, I'm making a 404, and I want it to display the current URL, but I don't want it to be server side. I don't even care about the variable after the URL, because I won't be using them, but it might be good to display it with the rest of the address. I noticed the link in the first post had that, but it was server side. Thanks!

Member Avatar for Member #671080

Hi
This thread was started in 2004!
Maybe you should start another thread of your own?

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.