0

i'm watching tutorials about making BLOG ,login and LOGOUT

after making Login and LOGOUT System i got some errors

From Those Lines in this file

header.php

<?php ob_start();?>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include "include/config.php";
global $tf_handle;
define("uid",$_COOKIE['uid']);

define("login",$_COOKIE['login']);

$QuerySelectU = mysqli_query($tf_handle,"SELECT * FROM user WHERE u_id ='".uid."'");
$FetchObjectU = mysqli_fetch_object($QuerySelectU);
#========================[USER]========================#
define("u_id",$FetchObjectU->u_id); // different because of cookies and Db
define("uname",$FetchObjectU->u_name);
define("uemail",$FetchObjectU->u_email);
#========================[USER]========================#
?>

part of

index.php

<?php include"files/header.php";?>
<?php
global $tf_handle;

if(isset($_POST['login']))
{
    $u_name  = strip_tags($_POST['u_name']);    
    $u_pass  = md5($_POST['u_pass']);   
    if(empty($u_name) or empty($u_pass))
    {
        echo"
            <div class='error'>Please Fill the form</div><br />
            ";      
    }
    else
    {
        $sqlquery = mysqli_query($tf_handle,"SELECT * FROM user WHERE u_name = '".$u_name."' AND u_pass = '".$u_pass."'");
        if(mysqli_num_rows($sqlquery) > 0)
        {
            $fetchLquery = mysqli_fetch_object($sqlquery);
            $uid = $fetchLquery->u_id;
            $uname = $fetchLquery->u_name;
            $upass = $fetchLquery->u_pass;

            if($uname != $u_name AND $upass != $u_pass )
            {
                //AND $upass != $u_pass
                echo"
                <div class='error'>wrong information</div><br />
                ";  
            }
            else
            {

                setcookie("uid",$uid,time()+60*60*24);

                setcookie("login",1,time()+60*60*24);

                echo"
                <div class='error'>you be redirected to home page</div><br />
                ";
                header("Refresh: 5; url=index.php");

            }
        }
        else
        {
            echo"
                <div class='error'>Wrong Information</div><br />
                ";              
        }
    }
}

When i Try To open index.php

i got some errors

Notice: Undefined index: uid in /var/www/html/Blog/files/header.php on line 8

Notice: Undefined index: login in /var/www/html/Blog/files/header.php on line 13

Notice: Trying to get property of non-object in /var/www/html/Blog/files/header.php on line 18

Notice: Trying to get property of non-object in /var/www/html/Blog/files/header.php on line 19

Notice: Trying to get property of non-object in /var/www/html/Blog/files/header.php on line 20

i got those errors after making Logout System

Logout.php

<?php include"files/header.php";?>
<?php



if(login == 1)
{
    setcookie("uid","",time()+60*60*24);

    setcookie("login","",time()+60*60*24);  
    echo"
    <div class='error'>Done </div><br />
    ";
    header("Refresh: 5; url=index.php");
}
else
{
    echo"
    <div class='error'>Wrong Page </div><br />
    ";  
}   
?>  
<?php include"files/block.php";?>
<?php include"files/footer.php";?>
smile
  • 117
  • 3
  • 16
  • I don't know what tutorials you have watched but they weren't the best. Better watch some of https://www.youtube.com/channel/UCpOIUW62tnJTtpWFABxWZ8g – SuperDJ Sep 09 '15 at 17:52
  • There are hundereds of tutorials out there on the web, and to be honest 95% of them are completely useless. Find another one. – RiggsFolly Sep 09 '15 at 17:53
  • i tried but somone told me they forgot to upload css file or something like that :/ and when i read the comments i saw 3 said the forgot file too..did you watched the tutorials ??? ..but anyway i want to know the reason and how to fix it :) – smile Sep 09 '15 at 17:54
  • Ok i will search but i need to know how he didn't got any errors only me !? i need some help to fix this – smile Sep 09 '15 at 17:54
  • inserting cookie `uid` into your query is a very bad idea, you are exploited by SQL injection – Andrew Sep 09 '15 at 17:56
  • @smile, css has nothing to do with php code, so don't worry about it... – Andrew Sep 09 '15 at 17:58
  • i know Andrew i was saying about the tutorials that he sent to me – smile Sep 09 '15 at 17:59
  • But did you figure out the reason of the error ??? i just want to know how to make login ..logout..register..profile for user to add it to my project :/ – smile Sep 09 '15 at 18:00
  • the first two warning codes are appeared because you dont have a cookie value with `uid` and `login`, therefore it cannot put the cookie value into define thus creating error. The last three lines said you are accessing an object variable, yet it is not an object so it creates error – Andrew Sep 09 '15 at 18:00
  • why it can't ?.......... – smile Sep 09 '15 at 18:02
  • when i try to login as user the error disappear – smile Sep 09 '15 at 18:04
  • @smile you will need to also show how you handle cookie ...that will be another question – Andrew Sep 09 '15 at 18:05
  • @smile I recommend you go through the basic of php before going into OOP, try understand the concept of variable and function first...these are the steps you cannot skip... – Andrew Sep 09 '15 at 18:07
  • i don't know :( i show you the code i don't know what do you mean with handling cookie i already show you the code – smile Sep 09 '15 at 18:08
  • i already know that @Andrew :) – smile Sep 09 '15 at 18:08
  • Read up a little on PHP sessions, try use those instead of regular cookies. Watch some tutorials from for example: The New Boston PHP tutorials – MrK Sep 09 '15 at 20:32
  • https://www.youtube.com/watch?v=rSHHshHAExw&list=PLVZUAHcV-erN_blX1ekzdTznwJx2NPeNM&index=2&spfreload=10 those tutorials good or not ?? – smile Sep 09 '15 at 21:22

1 Answers1

0

you set up the cookie after you logged in, so before logging in, you don't have a cookie value provided. But in your index.php define is try to access a cookie value which is not set, thats why it shows you the error and the cookie is not set until you logged in,therefore the message is dissappeared

Andrew
  • 2,810
  • 4
  • 18
  • 32
  • So you know what should i change the code ? :) – smile Sep 09 '15 at 18:19
  • @smile that will be in another question....you are asking why you get those error in this question – Andrew Sep 09 '15 at 18:21
  • hmmmm :D :D :D if i made another question you will tell me how to fix that ?? or you don't know ? – smile Sep 09 '15 at 18:22
  • and why he typed the same code and didn't get any errors :D !!!!! – smile Sep 09 '15 at 18:25
  • I wont guaranty but at least other people can take a look of it....and try to state your question clearly in your post...follow the SO guide to ask question properly, so other people will have less conflict when reading your question.. – Andrew Sep 09 '15 at 18:26