how can I dected if URL has variable?

Posted on 2016-11-09
Last Modified: 2016-11-09
Hello guys,

I have this code in my php file:

	$parc = $_GET['cod']; 	

Open in new window

the cod is a variable that comes from my URL, like:

but, sometimes the link doesn't have the variable, then I am getting an error:

Undefined index: cod

is there any way to prevent if URL came with variable or not?

Question by:hidrau
  • 3
  • 2
  • 2
LVL 33

Expert Comment

ID: 41880633
When it is a mandatory parameter, then use a redirect. Depending on your stack, this should be placed at the web server level, if possible.
LVL 109

Accepted Solution

Ray Paseur earned 500 total points
ID: 41880637
$cod = !empty($_GET['cod']) ? $_GET['cod'] : FALSE;
if ($cod) { /* YOU HAVE A VARIABLE IN THE URL */}

Open in new window

If you're new to PHP and want to learn the language, this article can help you get started.  Just skip over the parts you already know from other programming languages or computer science courses.

Author Comment

ID: 41880722
Hello Ray,

I had found something like this:

if isset($_GET['cod']) {
		$parc = 'VD001'
	} else {
		$parc = $_GET['cod']

Open in new window

What do you think about?

Thanks for the link
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 41880750
That looks a little bit backwards to me.  As I understand the intended logic, the external variable should be the controlling factor, otherwise a default value will be used.

The Ternary operator is useful for things like this.  This page is long, but it's required reading for anyone who writes PHP code.

Here is how I might write the expressions
$parc = !empty($_GET['cod']) ? $_GET['cod'] : 'VD001';

Open in new window

Here is the narrative:
1. Inspect $__GET['cod'] with the empty() function.  If that returns FALSE, there is information in $_GET['cod']
2. When empty() returns FALSE, !empty() [said "not-empty"] returns TRUE
3. With !empty() === TRUE, assign the value in $_GET['cod'] to $parc
4. With !empty() === FALSE, assign the literal string 'VD001' to $parc
LVL 33

Expert Comment

ID: 41880774
First of all: do you have a working backup? Do you have a test system?

Cause you currently allow SQL injection...


Author Closing Comment

ID: 41880808
Thanks a lot
LVL 109

Expert Comment

by:Ray Paseur
ID: 41880814
Thanks for the points.  I think ste5an may have a good point about the risks of SQL injection.  This article shows some ways of preventing it.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question