Undefined index: HTTP_USER_AGENT - php

I am trying to perform an action when a specific user agent visits a link.
So i have my code like this:
//if browser is not Mozilla/4.2, then do something.
//but if its Mozilla/4.2, do another thing.
if(strlen(strstr($_SERVER['HTTP_USER_AGENT'],"Mozilla/4.2")) <= 0 ){
// Do something
} else {
//Else do another thing code follows.
}
The above code is working but it keeps giving this warning in the error log "Undefined index: HTTP_USER_AGENT"
The solution i saw used pregmatch, but am targeting only a single user agent.
Any help will be appreciated.

You simply need to check the existence of the index (HTTP_USER_AGENT) on $_SERVER and if it's not set then set it to empty string.
This can be achieved by doing;
$userAgent = ! empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
if(strlen(strstr($userAgent,"Mozilla/4.2")) <= 0 ) {
// Do something
} else {
// Do something else.
}

Related

Warning on "$_SERVER['REQUEST_URI'] = MSU_REQUEST_URI;"

the line $_SERVER['REQUEST_URI'] = MSU_REQUEST_URI;
Fills my errorslog with
Use of undefined constant MSU_PHPEX_PS - assumed 'MSU_PHPEX_PS' (this
will throw an Error in a future version of PHP)
So I was thinking to solved it with $_SERVER['REQUEST_URI'] = 'MSU_REQUEST_URI';
Than is the warning gone but the script isn't working any longer.
Any ideas?
The Problem you're having is that MSU_REQUEST_URI is undefined
This means whatever value you expected the constant MSU_REQUEST_URI to have, has not been set at the time of you executing $_SERVER['REQUEST_URI'] = MSU_REQUEST_URI;,
By surrounding MSU_REQUEST_URI with quotes like this 'MSU_REQUEST_URI' you are assigning the String Value (literally) "MSU_REQUEST_URI" to $_SERVER['REQUEST_URI'].
So as #alithedeveloper has asked in the comments:
What is MSU_REQUEST_URI supposed to be and how/where is it supposed to get it's value from?
You won't be able to solve your issue without figuring out why MSU_REQUEST_URI is not set.
Thanks for the response I found a other page with code about the MSU_REQUEST_URI
<?php
if(defined('MSU_REQUEST_URI')) {
return;
}
if(version_compare(PHPBB_VERSION, '3.1', '>=')) {
global $phpEx;
// Fixing Symphony rewrite compatibility
$_SERVER['PATH_INFO'] = '';
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
define('MSU_REQUEST_URI', $_SERVER['REQUEST_URI']);
if(
in_array(
basename($_SERVER['SCRIPT_NAME']),
array(
'viewtopic.'.$phpEx,
'viewforum.'.$phpEx,
'search.'.$phpEx,
'memberlist.'.$phpEx,
'faq.'.$phpEx,
'viewonline.'.$phpEx,
'ucp.'.$phpEx
)
)
) {
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'].(!empty($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : '');
}
if(!defined('PHPBB_USE_BOARD_URL_PATH')) {
define('PHPBB_USE_BOARD_URL_PATH', true);
}
}
?>
Is that helping

CakePHP Notice (8) raised : Use of undefined constant inList - assumed 'inList'

Notice (8): Use of undefined constant inList - assumed 'inList' [CORE\Cake\Utility\ClassRegistry.php, line 168]
This notice has been bugging me for a while know, and I do not know how to fix it.. It was not really affecting my project earlier since its just a notice msg, but now, it is not letting me show an error message which I am trying to display to the user.
Iv got this function
public function validate_form(){
if($this->RequestHandler->isAjax()){
$this->request->data['Donor'][$this->params['form']['field']] = $this->params['form']['value'];
$this->Donor->set($this->data);
if($this->Donor->validates()){
$this->autoRender = FALSE;
}else{
$error = $this->Donor->validationErrors;
$this->set('error',$error[$this->params['form']['field']]);
}
}
}
The above is the action to which my post request submits to. Then it executes the following to display the error
if (error.length > 0) {
if ($('#name-notEmpty').length == 0) {
$('#DonorName').after('<div id="name-notEmpty" class="error-message">' + error + '</div>');
}
}else{
$('#name-notEmpty').remove();
}
The problem is that instead of the relevant error in my newly created div... I get that notice 8 from cake! Please if anyone knows why this is happening, I appreciate your aid on this one..
TLDR:
Do a project-wide find for 'inList' and find the spot where it either doesn't have quotes around it, or, if it's supposed to be a variable, is missing it's $.
Explanation:
You get that error when you try to use a PHP Constant that doesn't exist. Usually you're not actually TRYING to use a constant, but instead just forgot to wrap quotes around something or forgot to add the $ before a variable.
Examples:
$name = "Dave";
echo name; // <-- WOAH THERE, there is no Constant called name (missing $)
$people = array('Dave' => 'loves pizza');
echo $people[Dave]; // <-- WOAH THERE, no Constant called Dave (missing quotes)
Most likely somewhere else in your code you are using 'inList' as an array key but you don't have it quoted.
Example: $value = $myArray[inList];
It still works without quoting inList but it causes the notice message you're seeing.

Set cookie fails for first time but works on refresh

Though after reading explanations about setting cookie and not working for first time i find it difficult to resolve the below problem as am new to php and cookies.
I have a webpage with for (e.g) cp.php, login.php, header.php, maindata.php , bottom.php. Whenever i login to the webpage cp.php will be processed from there 1.header.php will be called first 2.maindata.php will be called and 3.bottom.php will be called.
So am setting my cookie at maindata.php and the code is like,
<?php
$cid = $_GET["id"];
$XmlPath = $_GET["path"];
$numpath = $_GET["numpath"];
$finepath =$_GET["finepath"];
$Tech =$_GET["tech"];
$read_str="";
function read($Path)
{
$temp="";
if(file_exists($Path))
{
$library = new SimpleXMLElement($Path,null,true);
foreach($library->children("SAS") as $info){
foreach($info->children("SAS") as $attributes){
$nameVal = $attributes->Name."=".$attributes->Value;
$str_temp .=$nameVal."#";
}
}
}else
{
$str_temp ="NA";
}
return $str_temp;
}
$arrpath =explode(",",$XmlPath);
/*Reading and storing arrpath[0] has the path of xml to be parsed*/
$strG=read($arrpath[0]);
$strC=read($arrpath[1]);
$strB =read($arrpath[2]);
setcookie($cid.'strE',$strG);
setcookie($cid.'comstr',$strC);
setcookie($cid.'basstr',$strB);
(....)
in the same file am reading the cookie using the below code,
$read_str =$_COOKIE[$cid.'strE'].$_COOKIE[$cid.'comstr'].$_COOKIE[$cid.'basstr'];
after this process is done bottom.php will be called and for the first time loading is completed.As i said for the first time am not getting any value in $read_str, but if i refresh the page and do all the process again i am getting the value.
As SETCOOKIE will return TRUE incase of successfully setting cookie i tried putting it in an if-loop and it returned false even for the first time.
kindly assist me in finding where the problem exists!
Make use of isset to check if a cookie exists and then try setting one.
Something like this.
if(!isset($_COOKIE['yourcookie'])) {
setcookie('yourcookie', 'Some data !');
$_COOKIE['yourcookie'] = 'Some data !';
}
echo $_COOKIE['yourcookie'];
I arrived here looking for an answer as well. Here's the deal.
When you set a cookie it can only be accessed on the next page load, that is why you can't access it after you set it. If you really need to work with the cookie data right away, you could set the value directly in global cookie such as:
$_COOKIE['my_cookie'] = 'i am a cookie';
Use setcookie()just the same so you can set expiration, domain, etc..

$write_result->errors throws "Undefined Property" when there are no errors

Using pre-written code from Bronto, it builds a soap client, calls a function on it, then parses the results. The parsing code looks like this:
if ($write_result->errors) {
print "There was a problem adding or updating the contact:\n";
print_r($write_result->results);
exit;
} elseif ($write_result->results[0]->isNew == true) {
print "The contact has been added. Id: " . $write_result->results[0]->id . "\n";
} else {
print "The contact's information has been updated. Id: " . $write_result->results[0]->id . "\n";
}
Whenever there ARE errors, they get caught and printed by the first if statement. But when there AREN'T errors, the console gets an "Notice: Undefined property: stdClass::$errors" message printed out. Is this right? Is there a way to turn off the notice? It doesn't cause any problems, but I can see how it would confuse a non-techy reading the output logs.
Check if the property exists instead of accessing it directly:
if (isset($write_result->errors))
Or to check if it exists and not empty at once (just to be sure in case the API changes and provides an actual empty array or empty string if no errors occured):
if (!empty($write_result->errors))
You could check first if the property exists:
if (property_exists($write_result, 'errors'))
Check that the property exists first:
if (property_exists($write_result, 'errors') && $write_result->errors)
{
// ...
}
See: property_exists.

PHP Undefined variable in Apache error_log

I'm getting a series of:
"Undefined variable: loginError in /Library/WebServer/Documents/clients . . ."
entries in my Apache error_log which I would like to prevent. I have a simple login.php page which, if there's an error logging in sets the $loginError variable as such:
$loginError = '<p class="text-error">Login Error: '. $layouts->getMessage(). ' (' . $layouts->code . ')</p>';
If there's no error logging in it does this:
$loginError = '';
I then output any errors as such:
if ($loginError !== '') { //line 112
echo $loginError; /line 113
}
I'm getting the entries for the line 112 and 113 noted in my comments above. Anyone tell me how I can prevent the entries appearing? I'm using PHP Version 5.3.6.
thanks
Its saying you should check it is set before using:
One way is with isset()
if (isset($loginError) && $loginError !== '') {
echo $loginError;
}
But in your particular case you may as well use !empty()
if (!empty($loginError)) {
echo $loginError;
}
Hard to say without seeing the rest of your code. Trace through your logic to make sure that every possible branch initializes loginError at some point in its execution. Even better, set it to a default value before you go through the logic.

Categories