Solved

how can I dected if URL has variable?

Posted on 2016-11-09
7
44 Views
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: www.teachmenow.com.br?cod=002

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?

thanks
alex
0
Comment
Question by:hidrau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 34

Expert Comment

by:ste5an
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.
0
 
LVL 110

Accepted Solution

by:
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.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
1
 
LVL 1

Author Comment

by:hidrau
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
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 110

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.
http://php.net/manual/en/language.operators.comparison.php

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
0
 
LVL 34

Expert Comment

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

Cause you currently allow SQL injection...

Capture.PNG
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 41880808
Thanks a lot
0
 
LVL 110

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.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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