Why is $_GET (PHP) not empty in new directories and files? [duplicate] - php

This question already has answers here:
WebStorm modifies URLs
(3 answers)
Closed 2 days ago.
In newly opened directories and files $_GET is not empty, by checking:
print_r($_GET);
var_dump($_GET);
if (empty($_GET)) {
echo '$_GET is either 0, empty, or not set at all';
} else {
echo '$_GET is not empty';
}
I get:
Array ( [_ijt] => mt4q815k0l6ke5gpmebq419g12 )
array (size=1)
'_ijt' => string 'mt4q815k0l6ke5gpmebq419g12' (length=26)
$_GET is not empty
Please explain the reason for this result. I use PhpStorm.

This occurs because your IDE has updated some patches. Try to run the application on an external server like XAMPP, not IDE built-in server.

Related

How to check if ANY php parameter exists in url [duplicate]

This question already has answers here:
PHP check if url parameter exists
(6 answers)
PHP to check if a URL contains a query string
(4 answers)
Closed 5 years ago.
I am making a forum that accesses threads based off the category in the URL using the GET method. I want to redirect to an error page if no parameters exist in the url, but I want this to be a generic piece of code that can be used around my whole site.
For example:
The url would normally contain the category id:
localhost/myforum/threads.php?categoryid=1
I want it so that when the url is:
localhost/myforum/threads.php
it is to redirect to an error page, and that this piece of code is usable all around the website
The most reliable way is to check if the URL contains a question mark:
if (false !== strpos($_SERVER['REQUEST_URI'], '?')) {
// There is a query string (including cases when it's empty)
}
Try:
$gets = parse_url($url));
if($gets['query'] == "")
{
echo "No GET variables";
}
Just:
if (empty(array_diff($_GET, ['']))) {
header("Location: /path/to/error.php");
}
EDIT: Updated to remove empty values
You can use is_set to check if the parameter exists like this,
isset($_GET)

how to check if string has server ih php [duplicate]

This question already has answers here:
Validating a URL in PHP [duplicate]
(3 answers)
Closed 6 years ago.
How to check if string has a server depending if url is absolute or relative?
function hasServer($url){
...
}
If this, returns true
hasServer('http://www.google.com/');
else return false
hasServer('/about-us/team/');
I think this is what you're looking for:
$url = 'http://www.google.com/';
$hasServer = filter_var($url, FILTER_VALIDATE_URL);
There are actually a few options from my experience. One would be to use the headers() function and then to analyse what information you obtained inside the resulting array.
$arrayResult = headers('http://www.google.com/');
foreach ($arrayResult as $value)
{
echo "-- ".$value."<br>";
}
The output should give you all the information you need regarding if the url actually exists.
A simpler solution in my opinion is to just check if the fopen() function actually works on the url!
if (fopen('http://www.google.com/', "r")
{
echo "the URL exists!<br>";
}
You can choose which one servers your needs better.

Getting Can't use function return value in write context [duplicate]

This question already has answers here:
Weird PHP error: 'Can't use function return value in write context'
(12 answers)
Closed 6 years ago.
I get the error in the subject. Also I spent ages on google and found dozens of resources having the same error, but still I can't figure out what the issue is.
This is my code:
<?php
if(empty(trim($_POST["user"])) || empty(trim($_POST["text"]))) {
echo "no luck";
}
?>
PHP Fatal error: Can't use function return value in write context in
/var/www/test.php on on line 2
If you refer to a manual, you will see
Determine whether a variable is considered to be empty.
The result of trim passed to empty is not a variable.
So your options are:
$user = trim($_POST['user']);
if (!empty($user)) { }
Or php5.5, in which
empty() now supports expressions

PHP adds \ to JSON [duplicate]

This question already has answers here:
"slash before every quote" problem [duplicate]
(6 answers)
Closed 9 years ago.
My shared web hosting adds \ to JSON. I use ExtJS and it normally sends this data
[{"property":"id","direction":"ASC"}]
Howere PHP receives or chages it as [{\"property\":\"id\",\"direction\":\"ASC\"}]
Thus I cannot use json_decode($_REQUEST['sort'])
I think this is because they wanted to prevent SQL injection but now they break my application. What I have to do?
Edit:
$sort = json_decode($_GET['sort']);
print_r($_GET); // [sort] => [{\"property\":\"id\",\"direction\":\"ASC\"}]
print_r($sort); //
Please check, if you vHost has magic quotes enabled.
Someone over here proposed this to get the unchanged values:
if (get_magic_quotes_gpc()) {
function strip_array($var) {
return ( is_array($var)
? array_map("strip_array", $var)
: stripslashes($var)
);
}
$_POST = strip_array($_POST);
$_SESSION = strip_array($_SESSION);
$_GET = strip_array($_GET);
}

I keep getting "undefined" when using the function $_GET in PHP, why is this? [duplicate]

This question already has answers here:
PHP $_GET and $_POST undefined problem
(5 answers)
Closed 2 years ago.
I continue to get undefined printed out when I use print($_GET['user_username']); from the previous page. The URL of the page is page.php?user_username=Pete. Why is this happening?
$_GET manual says
An associative array of variables
passed to the current script via the
URL parameters.
First be sure that element exists
<?php
echo !isset($_GET["user_username"]) ? "undefined" : $_GET["user_username"];
?>
Or try var_dump against $_GET array to see if element with user_username key exists.
var_dump($_GET);
Is your request like this one?
http://www.mydomain.com/something.php?user_username=something
Try this code:
print_r($_GET);
You will get all the elements passed using get in array format. Then you can check it.. It also helps better in debugging many times.

Categories