This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
Okay, so I am trying to build my site and I am having a minor issue. I am getting errors like "Undefined index: id in C:\xampp\htdocs\index.php on line 18"
and
"Undefined index: submit in C:\xampp\htdocs\index.php on line 39"
I am certain it's a matter of syntax but I can't seem to find the right answer. Here is the php code in my index.php file on lines 16-18
session_start();
if($_SESSION['id'] && !isset($_COOKIE['logRemember']) && !$_SESSION['rememberMe'])
The code worked before when I was running Apache 2 in linux. Now I am running Xampp on windows and all of a sudden it doesn't want to work right.
Whoever answers this, please write it in stupid. I'm still learning php. :) Thanks!
No, it's not a syntax error. You are trying to access a key that does not exist. It seems that warnings were disabled on your previous server.
Check if the actual variable is set (using isset) before trying to access it:
if(
isset($_SESSION['id']) &&
!isset($_COOKIE['logRemember']) &&
isset($_SESSION['rememberMe'] &&
!$_SESSION['rememberMe']
)
You use isset() on the $_COOKIE variable, so you must know how that works for checking the existence of a variable. Just apply that to $_SESSION['id'] and so on and the errors will stop.
In your first run, $_SESSION['id'] and $_SESSION['rememberMe'] won't be set.
You have to check if they're set (just as you did with your cookie)
if(isset($_SESSION['id']) && !isset($_COOKIE['logRemember']) && !isset($_SESSION['rememberMe']))
The reason it "worked" was because the Apache 2 server probably wasn't configured to display errors. Your issue is that $_SESSION['id'] isn't set. Rather, check that it is with
isset($_SESSION['id'])
Which won't throw an error if it isn't set.
Check the directory where the session data is storred is writeable by apache. You can get the path by using session_save_path ();
Replce line with following code
session_start();
if(isset($_SESSION['id']) && !isset($_COOKIE['logRemember']) && !isset($_SESSION['rememberMe']))
Related
we moved our website to a new server that came with a new IP address. What puzzles me; the website login sessions do not work on the new server but when I change the database IP to the old server they are working.
MySQL Version
Old server = 5.1.58- Community
New server = 5.1.68 - Community
At first I thought it was a PHP error but I now believe it's not and suspect its MySQL related. Anyone who knows what might have caused this conflict?
Debugging Error
Notice: A session had already been started - ignoring session_start() in C:\inetpub\wwwroot\gtest\libs\products.php on line 2
Notice: Undefined index: uUserTypeID in C:\inetpub\wwwroot\gtest\admin\index.php on line 50
Notice: Undefined offset: 0 in C:\inetpub\wwwroot\gtest\admin\index.php on line 52
Notice: Undefined offset: 0 in C:\inetpub\wwwroot\gtest\admin\index.php on line 52
Line 50
GetUserType($_SESSION['uUserTypeID'], $UserTypeID, $UserTypeDescr, $Active_Tag);
Line 52
if (($UserTypeDescr[0] == 'Admin') || ($UserTypeDescr[0] == 'Report'))
Let's go through the notices in order:
session_start() was already called before. No need to call it again.
There's no such variable as $_SESSION["uUserTypeID"]. It's not set.
The array at $UserTypeDescr doesn't have a 0 index. It's probably empty. If you got it from a database query, it either failed or returned an empty resultset.
Same as 3.
Notice: A session had already been started - ignoring session_start() in C:\inetpub\wwwroot\gtest\libs\products.php on line 2
At a guess session.auto_start is enabled hence the session is being enabled before the session handler is overridden (or the new session handler is failing).
The most safest method of preventing such error (i.e. Session already started) is by always checking if session has already been started before starting a session.
You can do this by simply including
if (!isset($_SESSION))session_start();
I hope this solve the problem? upon trying it, you can simply add your comment or observations to further improve the answer and above all, solve the probelm. I hope this helps?
There was a php project which was already in live server. Now my work is to change some style issues. So for that I just downloaded all the files from the server along with the database. After that I made all the necessary setup on my localhost(LAMP). Now when I browse the page I got some error like
Notice: Undefined variable: _session_register in path to the folder/file session.php on line 8
Now on line 8 I can see this code
$_session_register["esb2b_username"];
$_session_register["esb2b_userid"];
$_session_register["esb2b_memtype"];
$_session_register["esb2b_adv_id"];
$_session_register["esb2b_adv_email"];
$_session_register["lang"];
After searching over google I came to know that session_register in php is deprecated. So what will be the best solution to solve this issue in a fine way? Any help and suggestions will be appreciable. Thanks
There's a function called session_register that is deprecated, but what you have in the question is a variable. You are getting this notice because you are using array-access on it and it is undefined. This works, but it is better practice to define it as an empty array first.
$_session_register this is wrong use $_SESSION
session_register is a function which is deprecated. (Way which you written now is also wrong)
$_session_register["esb2b_username"]; change it to $_SESSION["esb2b_username"] = "";
I am seeing an undefined property warning PHP notice on my registration form in Joomla 2.5 , wondering whats causing this issue. Its happening on the lines which try to echo with $this
<?php if($this->title == 'Me'){echo "selected=\"selected\"";}?>>Me</option>
Update: $this in this case is referring to the correct class. Its the $title thats the issue which is prompting the undefined property issue. Whats the remedy to use variables in a php file which have not been defined yet?
Thanks.
As the message indicated that it is an undefined variable, after running xdebug I noticed that it was not being set and that was the issue. Thus checking if the variable is set and only then using the variable would help.
I am working on modification of a program. There are two value passes happened in one PHP file, and I get a notification from system like this:
Notice: Undefined index: content in /Users/alexhu/NetBeansProjects/menagerie/svn/trunk/apps/frontend/modules/legacy/legacy_lib/content/utilities/hraprint.php on line 23
And
Notice: Undefined index: isProvena in /Users/alexhu/NetBeansProjects/menagerie/svn/trunk/apps/frontend/modules/legacy/legacy_lib/content/utilities/hraprint.php on line 24
How to avoid it?
That error means you've basically got something like:
$my_arrray = array();
if ($my_array['content'] == ....) {
}
... attempting to access an index/key in an array which has not yet been defined. To guard against it, you'd need something like:
if (isset($my_array['content']) && ($my_array['content'] == ....)) {
Well you are trying to use an index of an array which doesn't exist.
The error is stating what is wrong :-)
You're simply trying to access $array['content'] and $array['isProvena'] in your code. Without the code we can't tell you anything though.
I'm new to PHP so I apologize if this is a simple problem...
I am moving a PHP site from one server to another. The new server is IIS 7.0, PHP 5.2.1, with short open tag turned "On", and I don't know how the original server was set-up (I was just given the code).
The following is the very first section of code on one of the pages:
<?
ob_start();
session_start();
if($_GET['confirm'] == 13 || $_GET['confirm'] == 14 || $_GET['confirm'] == 15 || $_GET['confirm'] == 16)
{
include("test/query/test_query.php");
}
?>
When this page executes, the following error is always shown:
PHP Notice: Undefined index: confirm in [file location].php on line 6
Also, users access this page by being redirected from the home page (which is a standard HTML page). The full URL when properly navigated to is the following:
http://www.[site].com/test.php#login
... I understand why the error is thrown. What I don't understand is how this code could ever work as it does on the original server. Could I be missing a configuration setting?
*This same issue happens in dozens of locations all over the site. This is just one specific occurrence of the issue.
The new server has error_reporting set to E_ALL. What you're seeing is a notice, not an error. Try:
error_reporting(E_ALL ^ E_NOTICE)
With error reporting set to E_ALL, accessing a member of an array which is not set generates an error. If you don't wish to lower your error reporting level, before checking $_GET['var'], change your code to:
if(isset($_GET['confirm']) && ($_GET['confirm'] == 13 || $_GET['confirm'] == 14 || $_GET['confirm'] == 15 || $_GET['confirm'] == 16)) {
by adding the call to isset() before you actually access $_GET['confirm'], you will verify that you're not accessing an array member which is not set. ($_GET['confirm'] will only be set if the URL ends in ?confirm=... or ?something...&confirm=...)
I suggest to optimize the code for reading:
if (isset($_GET['confirm']) && ($_GET['confirm'] >= 13 && $_GET['confirm'] <= 16))
And I totally agree with Josh's proposal.
Since there is no index $_GET['confirm'], PHP throws a notice that you are looking at an undefined index. The notice is being displayed because the new server has the E_NOTICE flag set in error_reporting somewhere, either in php.ini or in some config file or bootstrap that is run on pageloads.
From the php manual, E_NOTICE: "Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script."
You can either try turning off the notices if you aren't worried about them, or use them to track down places where there may be problems.
For the code you posted, an easy fix would be to change the conditional to
if(isset($_GET['confirm']) && <list of OR conditions>)
That way PHP bails out of evaluating the conditional if there is no 'confirm' index.
isset() is a useful function. It returns "true" if the variable exists and "false" if not. Usually, people use it in conjunction with a superglobal like $_GET or $_POST to determine whether you're being sent from another page on the site - this allows you to create different actions based on where your user is coming from and what data is tagging along. It also prevents errors in trying to use variables you haven't yet defined, like the OP is getting. So instead of needing to write two different .php files and worrying about sending your user to the wrong one, you can do it all in one page.
Jay,
I'd be careful about your usage of some of these calls. <?php is more likely to work than <? . I've heard session_start() should be the very first thing set to the browser or it can cause header issues. And yes, you need to have a variable declared before you use it - if you're not typing in [file].php?confirm=[some number] as your URL, your page will break unless you amend it to allow for breaks.
That's because confirm query string variable does not seem to be set, you can check it like:
ini_set('display_errors', true);
error_reporting(E_ALL);
var_dump($_GET['confirm']);