Undefined Index error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blindchild
Forum Newbie
Posts: 5
Joined: Sat Aug 13, 2005 10:47 pm
Location: London

Undefined Index error

Post by blindchild »

Hi,
I am relatively new to php and decided to learn by doing and so I made myself a sort of blog.
95% of the site works fine, but one problem that I am having is an undefined index error that is generated on one of my pages. Its quite annoying and messes up the whole layout of the page. The error is as follows:

Notice: Undefined index: KT_id_blindblog in /usr/local/psa/home/vhosts/blindchild.com/httpdocs/blog/byArticle.php on line 117

Now I think I know why the error is appearing, its because no one has actually logged into the page and so there is no value for the KT_id_blindblog session variable. Ive rattled my brains to try and work out how to fix the problem, but I am getting no where. Getting rid of the error one way will simply generate an error somewhere else.

The page can be viewed here: http://www.blindchild.com/blog/byArticle.php?id_art=4.

Any advice, suggestions or links would really help me out. I'm at a dead end here.

Thank you
Ross[/url]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hint:

Code: Select all

isset($_SESSION['KT_id_blindblog'])
blindchild
Forum Newbie
Posts: 5
Joined: Sat Aug 13, 2005 10:47 pm
Location: London

So close...yet so far

Post by blindchild »

Thanks for the hint, I did try that before but it generated another error when it actually came to a user logging in and posting.
Ill give it another go, thanks for the hint, it seems I'm close.
Thanks
Ross
PS. Ill let you know if I sort it. Thanks again.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

put it in an if/else

Code: Select all

if(isset($_SESSION['KT_id_blindblog']))
{
     // the session variable is set, so do this
} ELSE
{
    // the session variable is not set, so do this
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

... more along the lines of

Code: Select all

$_SESSION['KT_id_blindblog'] = (isset($_SESSION['KT_id_blindblog']) ? $_SESSION['KT_id_blindblog'] : 'default_value_goes_here');

//$_SESSION['KT_id_blindblog'] will always have been initialized by this point
echo $_SESSION['KT_id_blindblog'];
Last edited by John Cartwright on Sat Aug 13, 2005 11:44 pm, edited 1 time in total.
blindchild
Forum Newbie
Posts: 5
Joined: Sat Aug 13, 2005 10:47 pm
Location: London

Maybe....

Post by blindchild »

Hi again,
I just gave it another go and it appears to have worked. But just to check, this is what I did.
I inserted the line:

Code: Select all

if (isset($_SESSION['KT_id_blindblog'])){
before the default values are defined.
If I'm wrong let me know.
Thanks
Ross

**EDIT** Wow just saw some more poeple had posted. Ignore my attempt till Ive read yours, incase I have embarrassed myself. Thanks
blindchild
Forum Newbie
Posts: 5
Joined: Sat Aug 13, 2005 10:47 pm
Location: London

Both solutions work

Post by blindchild »

Hi,
Thanks for all your help, the page is functioning properly now.
Any ideas about which suggestion is the best? Because both seem to work fine...at the moment I am rolling with Jcart's suggestion, purely because he has more posts.
Thanks for the quick responses.
Ross
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that depends on how you want your code to function, but Jcart's will guarantee the variable existing.

Neither will guarantee the value being valid 100% of the time though, so you should still validate it.
Post Reply