Notice: Undefined index: id

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
marvela
Forum Newbie
Posts: 7
Joined: Wed Feb 18, 2015 9:18 pm

Notice: Undefined index: id

Post by marvela »

I am getting this "Notice: Undefined index: id" error message for my login system. I have tried several things to fix this but i have had no luck, i have used the most obvious solution, the isset function but i have had no luck. Please help.

This is the error message... Notice: Undefined index: id in /Applications/MAMP/htdocs/project/functions.php on line 4. The code for the functions page is below, if i need to include other pages please let me know.

Thanks in advance

Code: Select all


<?php

function is_logged_in(){
    if($_SESSION['id'] or $_COOKIE['id']){
        if($_COOKIE['id'] and !$_SESSION['id']) $_SESSION['id'] = $_COOKIE['id'];
        return $_SESSION['id'];
    }
    else
        return false;
}
function redirect_if_logged_in(){
    if(is_logged_in()){
        echo 'You are logged in. Redirecting!!';
        redirect2home();
    }
}
function redirect_if_not_logged_in(){
    if(!is_logged_in()){
        echo 'You are not logged in. Redirecting!!';
        redirect2home();
    }
}

function redirect2home(){
    die('<META HTTP-EQUIV="refresh" CONTENT="5; URL=personal.php">');
}

?>


User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Notice: Undefined index: id

Post by requinix »

isset() was the answer. What was the code you had which tried to use it?
marvela
Forum Newbie
Posts: 7
Joined: Wed Feb 18, 2015 9:18 pm

Re: Notice: Undefined index: id

Post by marvela »

I have tried using the isset function here " if($_SESSION['id'] or $_COOKIE['id']){ " but its not working.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Notice: Undefined index: id

Post by Celauran »

Code: Select all

if (isset($_SESSION['id']) || isset($_COOKIE['id'])) {
marvela
Forum Newbie
Posts: 7
Joined: Wed Feb 18, 2015 9:18 pm

Re: Notice: Undefined index: id

Post by marvela »

Thank you so much Celauran!!!

I have the same issue on lie 5 now... Notice: Undefined index: id in /Applications/MAMP/htdocs/project/functions.php on line 5

I have tired the asset function here and it shows me this message below...

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /Applications/MAMP/htdocs/project/functions.php on line 5
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Notice: Undefined index: id

Post by requinix »

marvela wrote:I have tried using the isset function here " if($_SESSION['id'] or $_COOKIE['id']){ " but its not working.
I was aiming more for what the actual code was. Can you post your actual code this time?

If you're not sure if the thing exists, use isset() on it. In that first check you don't know if either are set so you'd used it there (like Celauran showed). On the next line you still don't actually know if either exists - what you do know is that one of them does (just not which) so you need to use it there too.

Code: Select all

if (isset($_SESSION['id']) || isset($_COOKIE['id'])) {
	if(isset($_COOKIE['id']) and !isset($_SESSION['id']) $_SESSION['id'] = $_COOKIE['id'];
(You could actually just use "if(!isset($_SESSION['id']))" because you know that if the session id isn't set then the cookie must be.)
marvela
Forum Newbie
Posts: 7
Joined: Wed Feb 18, 2015 9:18 pm

Re: Notice: Undefined index: id

Post by marvela »

I have fixed the errors thanks for your help!
Post Reply