0

I am just stuck today at a wall of confusion and I'm hoping someone will be able to assist :)

I have a database full of basic projects, and within that table are attributes like Project Name, Project Number, Project Image, etc. I am able to enter new projects / display existing projects / etc. without issue.

My problem seems to come up when I want to Edit a project. My thoughts were that I would have to create an IF statement to find out if there's a new file uploaded or not, and either set the new file name in the database if there is, or keep the old in the database if there isn't.

I've been playing around with this for days, and I think I started off a bit too far ahead of myself. I've started breaking down to basics and I'm getting stumped with my IF statement, it feels like it's backwards? Does this make sense?

Examples:

if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

if(isset($OldProjectImage1)){ 
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}

Now in searching StackExchange I found that we have to do the statement on the COOKIE portion instead of the Variable as I did above, but it also similarly fails.

if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

if(!empty($_COOKIE['OldProjectImage1'])){ 
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}

And I've also tried with isset

if (isset($_COOKIE["OldProjectImage1"])){$OldProjectImage1 = $_COOKIE["OldProjectImage1"];}

if(isset($_COOKIE['OldProjectImage1'])){ 
echo 'Your Browser Cookies are not enabled';
} else if(isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES isset';
} else if(!empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES empty';
}

I've tried both with my script, and they're both behaving similarly. Perhaps I am just confused at the overall process?

When I run my tests with and without cookies enabled, it always seems to skip the first part of the IF statement (with both isset and !empty) and jump to the next section. Then, similarly, it feels like the IF statement is backwards (if that makes any sense) - if I set a file to upload which populates ProjectImage1, I get "Image1 FILES empty". If I set no file to upload and submit the form, I get "Image1 FILES isset".

I thought it would essentially be, in plain English,

If cookie is empty then echo "Your Browser Cookies are not enabled"
Else if ProjectImage1 Name is set, echo "Image1 FILES isset"
Else if ProjectImage1 Name is Empty, echo "Image1 FILES empty"

but it's feeling to me like it's backwards? Am I understanding this wrong?

Thanks in advanced for any responses!

cwaddilove
  • 67
  • 1
  • 9
  • You have to set a cookie first before you can read it. But a cookie is not related to a database, not is it related to uploading files. So actually, I don't understand what you are trying to do. – GolezTrol Jul 23 '15 at 18:09
  • 1
    Try taking the `!` away from `if(!empty(`. `empty()` will return true if the variable exists, but is empty. At the moment, writing `!empty` will return `true` (and therefore pass the if condition), if the variable contains something. – Phil Cross Jul 23 '15 at 18:10
  • What is the output of var_dump($_COOKIE["OldProjectImage1"]); – danbahrami Jul 23 '15 at 18:18
  • Sorry GolezTrol, I should have provided more insight into the overall project, however I had a feeling it was something silly directly related to those IF statements as Phil Cross mentioned. I wish I could mark a comment as an answer - that's exactly what I needed Phil! Thanks for the explanation! I'm still new to lots of PHP and I've been hacking things together from examples but didn't realize the difference between adding or removing the ! – cwaddilove Jul 23 '15 at 18:18

2 Answers2

2

Problem lays with:

if(isset($_COOKIE['OldProjectImage1'])){ 
echo 'Your Browser Cookies are not enabled';
}

You check if the cookie exist, and if it does, then you say that cookies are not enabled. A bit weird. Add ! before the isset. Then the if-statement and the text are correct.

I think, but I can only assume, you want this in the end:

if (isset($_COOKIE["OldProjectImage1"])){
    // I believe the variable below can also be put between the else { and } down below
    $OldProjectImage1 = $_COOKIE["OldProjectImage1"];
}

if(!isset($_COOKIE['OldProjectImage1'])){ 
echo 'Your Browser Cookies are not enabled';
} else if(!isset($_FILES['ProjectImage1']['name'])){ 
echo 'Image1 FILES is not set';
} else if(empty($_FILES['ProjectImage1']['name'])){
echo 'Image1 FILES is empty';
}else {
    // upload file here
}
Verkade89
  • 373
  • 2
  • 12
0

I think you want to check the browser cookie is enabled or not?

Detect if cookies are enabled in PHP

Answer by Shiplu Mokaddim:

session_start();
if (isset($_GET['check']) && $_GET['check'] == true) {
    if (isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
        // cookie is working
        // get back to our old page
        header("location: {$_SESSION['page']}");
    } else {
        // show the message "cookie is not working"
    }
} else {
    // save the referrer in session. if cookie works we can get back to it later.
    $_SESSION['page'] = $_SERVER['HTTP_REFERER'];
   // set a cookie to test
    setcookie('foo', 'bar', time() + 3600);
    // redirecting to the same page to check 
    header("location: {$_SERVER['PHP_SELF']}?check=true");
}

Detect cookie in Javascript

Check if cookies are enabled

So, combine with your code and my own explanation:

<?php 
    session_start();
    //check if a cookie test is started
    if (isset($_GET['check']) && $_GET['check'] == true) {
        //cookie test is started
        if (isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
            //cookie test success, go back to the previous page
            header("location: {$_SESSION['page']}");
        } else {
            //cookie test fail, echo the message and continue
            echo 'Your Browser Cookies are not enabled';
        }
    } else {
        //start cookie test if a cookie test wasn't done
        //check if a cookie test was done.
        if (!isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
            //start a cookie test if a cookie test wasn't done
            $_SESSION['page'] = $_SERVER['HTTP_REFERER'];
            setcookie('foo', 'bar', time() + 3600);
            header("location: {$_SERVER['PHP_SELF']}?check=true");
        }
    }
    if(!isset($_COOKIE['OldProjectImage1'])){ 
        echo "OldProjectImage1 doesn't exists in cookies.";
    } else if(!isset($_FILES['ProjectImage1']['name'])){ 
        echo "Image1 FILES is not set";
    } else if(empty($_FILES['ProjectImage1']['name'])){
        echo "Image1 FILES is empty";
    }
?>
Community
  • 1
  • 1
attempt0
  • 639
  • 5
  • 14