How to check if a field is blank or not? - php

So I have this code in PHP, and it's not working for some reason that I don't know:
if(!empty($ccle))
{
header("Location: http://google.com");
}
else
{
header("Location: http://yahoo.com");
}
What I want is: if the "ccle" field is empty, then go to google.com
Or if the "ccle" field is not empty (Where ever was the value) then go to yahoo.com
How to make it do that?

You need to switch them...
if(!empty($ccle))
{
// Go to Yahoo if $ccle is NOT empty
header("Location: http://yahoo.com");
}
else
{
// Else, go to Google
header("Location: http://google.com");
}
Just to elaborate. What your if statement is saying is...
if($ccle IS NOT empty)
The exclamation in PHP is the logical not operator.

If you'd like to redirect to Google when it's empty and Yahoo when it's not. You would then change:
if(!empty($ccle))
To:
if(empty($ccle))

if(empty($ccle)) {
header('Location: http://google.com/');
} else {
header('Location: http://yahoo.com/');
}
This should perform as you described. The only difference is I've removed the exclamation. Your original code said if ccle is ndt empty, go to google. This says if it is empty go to google.
You have to make sure this is called before anything else is output to the browser. If it's still not working post any errors or what the output actually is.

All the other answers are spot on
i'd just like to add the the ! part of !empty signifies "NOT", so what you've been typing in is essentially "if the field is NOT empty, then go to google" instead of "if the field is empty, then go to google"
also, how are you getting the variable? if you have a form similar to this:
<input name="ccl" type="text" id="ccl" />
and something similar to this for grabbing the form contents and putting it into a variable,
<?php
//making sure the forms button is pressed
if(isset($_POST["button"])){
//grabbing input and putting into a variable
$ccl = ($_POST["ccl"]);
}
?>
then the other answers provided should work

Related

PHP if specific value then continue

I have made a form, where that if a value is missing the redirect to error page.
<?php
if (empty($_POST['submit']) && empty($_POST['email']))
{
header('Location: empty.php');
exit;
}
?>
Now, Lets say that i have a password that i need my users to type in to proceed, how do i do that, and if its wrong, then how do i redirect them to empty.php?
My problem is that i dont know how to make a "PHP" tag that redirects if not specific value.
Solution (please, read whole post, as there are security holes..., even black holes in the code ;) ):
<?php
if (isset($_POST['password']) && $_POST['password'] == 'yourpasshere')) {
// Password OK, continue
}
else {
// Password not ok, redirect
header('Location: empty.php');
exit;
}
?>
Few words about solution: if $_POST['password'] is not set, the second condition is even not checked, and it go to else. If the $_POST['password'] is set, second condition check if the password is as you want (read security warnings below). If and only if it is OK, the if passed.
There are few important messages however:
1) Never store password in plain text, anywhere! Read this: http://php.net/manual/en/function.password-hash.php. My example is storing password in plain text, but it is only to sketch the possible solution. Every possible leak of your file gain access to your site.
In that case, my answer sketch the logical part (in which you have a problem) of solution, but NEED TO BE CORRECTED IN SENSE OF SAFETY.
2) Always validate form data (minimal solution is to use http://php.net/manual/en/function.htmlspecialchars.php, but this is not perfect, and it is much more better to use preg_match, or even preg_replace for special characters, to remove them
2*) You can validate email using: var_dump(filter_var('bob#example.com', FILTER_VALIDATE_EMAIL)); (doc: http://php.net/manual/en/function.filter-var.php). This is a great function that will try to validate if entered email is really OK.
3) In your code it will redirect to empty page only if BOTH of the POST values are empty. If You want to redirect if any of them is empty use logical OR (||) not AND (&&).

If either GET or POST are empty, redirect

Very strange problem and I'm scratching my head. If anyone can help it would be much appreciated, thanks.
I have a Search Location result page that should only appear if either 1) the form on the previous page has been submitted; or 2) they are searching via a url, e.g.
www.mywebsite.com?use_url=on&zipcode=UB95BX&radius=50.
So, at the top of the page I have the following code:
// Validate input and sanitize
if (empty($_POST['submit']) || empty($_GET['use_url']))
{
header('Location: index1.php');
exit;
}
Problem is, it's not working and it's redirecting EVERY request. Does anyone know how I can get it to work, so that if there is no submit post, or if there is no get request (basically, if the user types in www.mywebsite.com/locations.php directly into the url bar), the user is redirected.
Strange thing is this, if I leave out the empty($_GET) command and make it therefore
// Validate input and sanitize
if (empty($_POST['submit']))
{
header('Location: index1.php');
exit;
}
It works fine on an empty submit, the problems only occurs when I specify two criteria for a redirect.
Thanks everyone / Luke
|| is logical OR. Currently your code reads "if either post or get are empty, redirect"
What im sure you mean is "if both post and get are empty, redirect".
You should use logical AND (&&):
if (empty($_POST['submit']) && empty($_GET['use_url']))
{
header('Location: index1.php');
exit;
}
Try this:
if (empty($_REQUEST['submit'])
or if (!isset($_REQUEST['submit'])

Allow PHP page only if set the correct url

I am trying to figure out how to allow PHP index only if it matches the URL I put in, something like this:
Example if you put in URL site.com/myfile.php I want to show message like 404 Error or something
But if you put site.com/myfile.php?=123 to show the page content.
I think it might be considered bad practice to send someone to a 404 when the page that they are accessing does actually exist, it's only a variable that they are missing.
Firstly, I'd expect to see something like
site.com/myfile.php?variablename=123
instead.
If you absolutely wanted to, you could at the top of your file then add a:
if(!isset($_GET['variablename']){
header('location:404.php');
}
Where 404.php is your 404 file that you'd like the user to see.
Hope that helps?
You could run a conditional looking for the request info, i would make it specific like using a ?page_id=123
<?php
if ( $_REQUEST AND isset($_REQUEST['page_id']) )
{
// SHOW PAGE CONTENT
}
else
{
// RETURN 404
}
?>
You would have to test the $_GET parameters.
if (!$_GET) {
echo "404 Error"; //or redirect using header();
} elseif ($_GET['key'] == 'value') {
//code here
}
I see you are using ?=123, I'm not 100% on if that will work, but it's easier (IMO) to have a key=>value association in the URL.
This should be enough for what you need. If there are other requirements let us know.
if(!isset($_GET)){
//do whatever you want to validate the get input provided.
} else {
header("HTTP/1.0 404 Not Found");
}
Well first of all this ?=123 might be an issue because when ?=123 is passed then it should be stored somewhere, it could be like this ?uid=123 and then you retrieve it in a variable through the get method and then check its value and accordingly redirect the user.
You can do something like this
if(isset($_GET['uid']))
{
refresh(to whatever location you want);
}
else
refresh(to some other location);
but if you want the error 404 something then its not possible according to me, because the values will be sent to a page that exists and if the page exists then the server cannot give a "not found" error.
By simply comparing a string.
if (#$_GET['secret'] != 'mysecret'){
header('location: noaccess.php'); //redirect the user to access denied page.
die(); // terminate the script. A
}
// The rest of the page
Or to keep everything in one file:
if ((isset($_GET['secret'])) && ($_GET['secret'] == 'mysecret')){
// Show the page
} else {
// Show an error message
}
site.com/myfile.php?secret=mysecret

How to add a GET variable to the current URL?

I have a question, i want to make some search page, and it needs a get variable to sort the results. So if someone enters the page without that GET variable, reload the page and make it appear, for example you enter www.myweb.com/search and automatically reloads and changes to www.myweb.com/search/?sort=ascending (because that variable is necessary) .
I hope you understand me, good bye
I think this will work for what you're looking to do:
if (empty($_GET['sort'])) {
header('Location: ' . $_SERVER['REQUEST_URI'] . '?sort=ascending');
exit();
}
From within the file executed when www.myweb.com/search is requested, you should have a default setting when $_GET['sort'] isn't available. For this Answer, I'll be using PHP for my examples since you didn't specify.
<?php
if (empty($_GET['sort'])) {
$sort = 'ascending';
}
// the rest of your code
Alternatively, you could force a redirect, but the previous example is more elegant.
<?php
if (empty($_GET['sort'])) {
header('Location: www.myweb.com/search/?sort=ascending');
exit;
}
Keep in mind, the second solution would throw away anything else, i.e., other $_GET's, like item=widget or color=blue
Note to others posting !isset as an answer. That will not work! Example:
www.myweb.com/search/?sort=&foo=bar
!isset($_GET['sort']) === false!
empty($_GET['sort']) is the proper route to take in this circumstance.
It is better to define the variable by yourself rather then redirecting. Just check with isset if the variable is defined or not. It it has not been defined you can set it yourself as below.
if(!isset($_GET['sort']))
{
$_GET['sort']='ascending";
}

php returning innerHTML

Im trying to update the contents of an element after running some php code. I realize the php is executed first, but I thought by loading the page I could then find the element? However console says cannot find element of null so I guess the page isn't loading before the innerHTML code is running.
Anyone any ideas?
else if(strlen($_POST['username']) < 6){
header("Location: http://webpage/register.html");
echo "document.getElementById('elemID').innerHTML = usename too short";
}
header() instructs your clients to go to the new location, hence outputting anything after that would make no effect to your client as the content of register.html is already handled differently by your server.
If you can change register.html to use php instead, you could pass
header("Location: http://webpage/register.php?msg=username%20too%20short");
Then in your register.php
if(!empty($_GET['msg'])) echo $_GET['msg'];
First, you shouldn't really have any logic after your header Location redirect. It is good practice to put "exit" or "die" after a redirect like that as you can't guarantee that the browser will ever see the next line before redirecting. In fact, you can pretty well guarantee that it will more often not see that code.
If you're going to redirect, put your error as an argument to your redirect URL and have logic there that shows the error like this:
header("Location: http://webpage/register.php?error=username%20too%20short");
Then in you register.php (I renamed it from .html so you can read the error argument) you can reference your error like:
$error = $_GET['error'];
if (!empty($error)) {
//write your error out in some markup or javascript...
}

Categories