terryds Posted September 26, 2013 Share Posted September 26, 2013 (edited) See my code first <?php include(dirname(dirname(__FILE__)) . "/pathdefiner.php"); include_once (BASEPATH . INC . "logged.php"); if (!isLoggedIn()) { include ("templates/login.html.php"); exit(); } if (isset($_GET['addp'])) { $title = "Add post"; $desc = "Type down what your post want to be"; $action = "?addpost"; $id = ''; $titlepost = ''; $button = 'Add post'; $editmode = FALSE; include "templates/form.html.php"; die(); } if (isset($_GET['addpost'])) { if (empty($_POST['post']) || empty($_POST['title'])) { echo 'Please fill and set the post entry' . '<br>' . '<p><a href=".">Go Back</a></p>'; die(); } include_once (BASEPATH . INC . "connect.php"); try { $sql = "INSERT INTO `posts` SET `title` = :title, `posttext` = :post, `posttime` = :time"; $s = $pdo->prepare($sql); $s->execute( array( ':title' => $_POST['title'], ':post' => $_POST['post'], ':time' => time() ) ); } catch(PDOException $e) { die("Error adding post. Error: " . $e->getMessage()); } header('Location: .'); die(); } include "templates/admin.html.php"; include "templates/logout.inc.html.php"; After executed, it generates an error that the $pdo variable is not set... But, if i change the include_once to include, it will work well... And, if i change the $pdo variable in my connect.php to a global scope, it will work well too... Can you tell me why this happens? Why if i change include_once to include, it will work ? Why if i set the $pdo variable to global, it will work ? Which's better ? Change include_once to include or set the global scope for $pdo ? This is my connect.php <?php require_once (dirname(__FILE__) . '/config.php'); define("DSN", 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME); try { $pdo = new PDO(DSN, DB_UN, DB_PW); $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $pdo->exec('SET NAMES "utf8"'); } catch (PDOException $e) { echo DB_PW . "<br>"; die('Error: ' . $e->getMessage()); } Edited September 26, 2013 by terryds Link to comment Share on other sites More sharing options...
justsomeguy Posted September 26, 2013 Share Posted September 26, 2013 It sounds like the file is being included somewhere else, and it gets included inside of a function. Therefore, the variable is defined inside that function that includes the file and is not global, and since it was already included once it doesn't get included again. Link to comment Share on other sites More sharing options...
terryds Posted September 27, 2013 Author Share Posted September 27, 2013 So, what should i do ? Make it global scoped or change the include_once with include ? I think that the file including connect.php is my logged.php <?php function isLoggedIn() { if (isset($_POST['action']) and $_POST['action'] == 'login') {if (empty($_POST['un']) || empty($_POST['pw'])) {$GLOBALS['loginError'] = "Please fill in both fields";return FALSE;} $pw = crypt($_POST['pw'], '$2a$07$427fyb79dy4r948n80dimsillYs$l7$'); if (dbContainsUser($_POST['un'], $pw)) { session_start(); $_SESSION['loggedIn'] = true; $_SESSION['un'] = $_POST['un']; $_SESSION['pw'] = $pw; return TRUE; } else { session_start(); unset($_SESSION['loggedIn']); unset($_SESSION['un']); unset($_SESSION['pw']); $GLOBALS['loginError'] = "Invalid username or password"; return FALSE; }} if (isset($_POST['action']) and $_POST['action'] == 'logout') {session_start();unset($_SESSION['loggedIn']);unset($_SESSION['un']);unset($_SESSION['pw']);header('Location: .');die();} session_start(); if (isset($_SESSION['loggedIn'])) {return dbContainsUser($_SESSION['un'], $_SESSION['pw']);}} function dbContainsUser($username, $password) {include "connect.php"; try {$sql = "SELECT COUNT(`id`) FROM `users` WHERE `username` = :un AND `password` = :pw";$s = $pdo->prepare($sql);$s->execute(array(':un' => $username,':pw' => $password ));} catch (PDOException $e) {$error = "SQL ERROR = Error searching for user";echo $error;die();} $row = $s->fetch(PDO::FETCH_ASSOC); if ($row['COUNT(`id`)'] > 0) {return TRUE;} else {return FALSE;}} Link to comment Share on other sites More sharing options...
justsomeguy Posted September 27, 2013 Share Posted September 27, 2013 Right, since you included that file inside the dbContainsUser function, the $pdo is local to that function, not global. Only include the file again if you need an additional database connection. Otherwise, it makes sense to include the file globally and then use the global variable for everything that can use the same database connection. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now