3

I've came across this problem before, but I fixed and it was actually simple. I can't remember what I did to fix it.. :/ It's not showing me as premium at the bottom of my page, my purchase goes through, it just doesn't update because it says, 'trying to get property of a non object' at the top of my page. EDIT: It also doesn't update in the database, but when I update manually it still doesn't show I'm premium on the webpage.

Here is where the problem is, index.php

    <?php if($users->premium): ?>
    <p id="prem" style="position: fixed; bottom: 0; width:100%; text-align: center">You are <span style="color:#FB0307">PREMIUM</span>!</p>
    <?php else: ?>
    <p id="prem" style="position: fixed; bottom: 0; width:100%; text-align: center">You are not premium. <a href="checkout.php"><span style="color:#FB0307">Go premium!</span></a></p>
    <?php endif; ?>

To fix this problem, this is the code I'm pretty sure I had to edit the last time. 'It was actually something really small' lol

    $_SESSION['user_id'] = '';

$stripe = array(
    "secret_key" => "sk_test_hzpYzgCFLBf3sTFtqzYX8Qqy",
    "publishable_key" => "pk_test_AdvDAYfsO9vdYBXJiPaucNHw"
    ); // Test keys

    \Stripe\Stripe::setApiKey($stripe['secret_key']);

    $db = new PDO('mysql:host=127.0.0.1;dbname=blog', 'root', '');

$userQuery = $db->prepare("SELECT id, username, email, premium FROM users WHERE username = :username");

    $userQuery->execute(array(':username' => $_SESSION['user_id']));

    $users = $userQuery->fetchObject();

I highly doubt this has anything to do with it, but this creates the $_SESSION at the top of the page for the 2nd code block. There is more than this if($login) statement, but this is the only statement pertaining to this.

if($login){
            $_SESSION["user_id"] = Input::get('username');
             Redirect::to('index.php');
            }else{
                 echo '<p style="color: white;">Sorry, login failed.</p>';
        }
David
  • 184
  • 15
  • 1
    According to your code the error `trying to get property of a non object` relates to the `$users->premium`. `$users` isn't an object from some reason - start with `var_dump($users)` to debug it. It might be that `$userQuery->fetchObject` returns `false`. – Ofir Baruch May 18 '15 at 05:59
  • Exactly what @OfirBaruch said in his comment. You must first make sure that the query was successful. Is your `$_SESSION['user_id']` set? do you have `session_start();` at the top of every page you use sessions in? – Darren May 18 '15 at 06:06
  • I don't understand why it worked recently though :/ I used `var_dump` , it does return false, ' bool(false) ' How can I fix this? – David May 18 '15 at 06:12
  • Yes, I have a session started, sir. It's not on that page, but I have one started. Does that matter? – David May 18 '15 at 06:13

1 Answers1

1

Your issue is due to the fact that your $users variable is not an object. Meaning that your query failed.

Which would lead me to believe it's because of the $_SESSION['user_id'].

We can't tell from your code supplied, but you always need to start the session on each and every page you want to use sessions on:

session_start();

Also, you set the user session id to empty before you run your query...

$_SESSION['user_id'] = '';

Which would warrant your query useless because now the user_id is empty.

Darren
  • 13,050
  • 4
  • 41
  • 79
  • The user id is null in my database though? I figured that should be empty, if it wasn't then the only way people would get premium, is if I typed their ID in everytime they wanted to purchase.. – David May 18 '15 at 06:17
  • @David Why would you set a user_id to `NULL`? Thats a unique identifier.. you need to know who the user is – Darren May 18 '15 at 06:18
  • I didn't mean NULL, I meant auto increment, sorry. I didn't have the = on user id at first and it gave me an error, I changed it to = '' , & it took the error away, I did this a few days ago, it just now started doing this. I thought I fixed it! :( – David May 18 '15 at 06:23
  • @David what does `var_dump($_SESSION['user_id']);` give you? – Darren May 18 '15 at 06:32
  • It returns string(0) "" – David May 18 '15 at 06:34
  • And what does `var_dump($users)` return? – Darren May 18 '15 at 06:35
  • Can't use function return value in write context – David May 18 '15 at 06:38
  • I think I changed `$user` to `$users` when I first got an error and it worked fine. I can change it to `$user` now and I get Undefined property: User::$premium , which I can remember getting that error and couldn't figure a way around it without adding more errors, so I used `$users` and it worked fine. Guess that's what I get for half doing stuff. :| EDIT: I used `var_dump` on `$user` and got the same thing. – David May 18 '15 at 06:43