I know this is little bit strange but some variables are undefined when including these variables.
This is my web structure:
details.php:
$login['server'] = 'server';
$login['database'] = 'db';
$login['username'] = 'user';
$login['password'] = 'pass';
define ('constant1' , 'ok');
main.php:
include ('subfile.php');
subfile.php:
echo $login;
echo constant1;
index.php:
include ('details.php');
include ('main.php');
And output is undefined $login and ok. I already tried with get_included_files() in subfile.php and this function shows details.php as included file.
Related
This is my first page.I just can't get my variables to the next page index.php ($_SESSION['admin_email']). The variable assignment works fine on the first page, in index.php it always redirected to login.php
<?php
error_reporting(0);
session_start();
include_once('../_inc/_class/Users.php');
$dbUsers = new Users();
$uname = htmlspecialchars($_POST['username']);
$upass = md5($_POST['password']);
if($uname != "" && $upass != "") {
$user = $dbUsers->selectAll("`password` = '".$upass."' and `email` = '".$uname."' and user_type = 'admin'", "1", "");
/*print_r($user);*/
if(!empty($user) && $user[0]->email == $uname && $user[0]->password == $upass) {
$_SESSION['admin_email'] = $user[0]->email;
/*var_dump($_SESSION['admin_email']);*/
}
else {
$_SESSION = array();
session_destroy();
}
}
die(header('Location:../index.php'));
?>
index.php
<?php
error_reporting(E_ALL);
session_start();
if(!isset($_SESSION['admin_email'])) {
die(header('Location:./login.php'));
}
?>
From your code, it looks like login.php is residing inside a subfolder and not in the root folder where index.php reside.
You are setting the session value from the subfolder and if your php.ini settings or some session_set_cookie_params() in included class (Users.php) is setting the session path deliberately for that subfolder, it will make the session variable inaccessible outside that.
So try to add session_set_cookie_params and set the $path param to /
And then it will be available for the root folder and all subfolders inside that.
For more information check session_set_cookie_params
use
if(!session_id()) session_start();
instead of using plain
session_start();
I will describe my problem in two parts (previous problem and current problem).
Previous Problem:
Initially, on page3.php, I wasn't able to retrieve the username using the session variable and hiding //require('../myDBFolder/db.php'); solved the problem and I was able to see the username on that page.
Current Problem:
Since, I have commented out the line //require('../myDBFolder/db.php');, I am not able to access the other variables defined in db.php like $connection variable and hence I am trying to figure out how to make sure I have $connection variable available in page3.php.
A Quick explanation of the working of files is in the following order:
User submits username from page1.html, page2.php does the authorization work with db.php as required file and upon successful authorization, it directs the user to page3.php.
Please consider my files below:
page1.html
<form method="post" action= "page2.php" name="lform">
<span class="style1">User Name :</span>
<input type="text" name="user" size="25">
<input type="submit" value="login">
</form>
db.php
<?php
session_start();
$user = $_POST["user"];
$_SESSION['username']=$user;
$db_server = "localhost";
$db_name = "PracticeDB";
$db_user = $user;
$table_name_data = "collegestudents";
$connection = mysqli_connect($db_server,$db_user,$db_password) or trigger_error("Could Not Connect to the Database : ". mysqli_connect_error(), E_USER_ERROR);
$db = mysqli_select_db($connection , $db_name) or trigger_error("Could Not Select the Database : " . $db_name . ':' .mysqli_error($connection));
?>
page2.php
<?php
session_start();
require('../myDBFolder/db.php');
$user = $_POST["user"];
$_SESSION['username'] = $user;
$sql="SELECT * FROM $table_name_users WHERE username = \"$user\"";
$result=mysqli_query($connection,$sql) or trigger_error("Couldn't Execute Query in page2.php: ". mysqli_error($sql));
$num = mysqli_num_rows($result);
if ($num != 0) {
print "<script>";
print "self.location='page3.php';";
print "</script>";
} else {
echo "<p>you're not authorized";
}
?>
page3.php
<?php
session_start();
//require('../myDBFolder/db.php');
$user = $_SESSION['username'];
$sql = "SELECT * FROM $table_name_data WHERE username = '$user'";
$result = mysqli_query($connection,$sql) or trigger_error("Could Not Execute the Query ! : ". mysqli_error($connection));
?>
Troubleshooting Steps:
1) I have tried to include require('../myDBFolder/db.php'); in page3.php file and it solves the problem of $connection parameter but I don't see username coming onto that page via session for some reason and also by including //require('../myDBFolder/db.php'); in page3.php I will be making db connection twice as I have already done that in page2.php and haven't closed it.
2) Another thing, I was looking at some of the threads discussed before like this one, it seems like storing $connection in a session variable is not a good idea.
Just to point in a direction:
Change this
$user = $_POST["user"];
$_SESSION['username'] = $user;
to
if(isset($_POST["user"])){
$user = $_POST["user"];
$_SESSION['username'] = $user;
}
So, only update the SESSION if POST is given.
By the way, it is not good practise to give each user an db user account.
Your SQL check if a user is in the database, but your connectin also uses this username!? Rething that..
If you only use one db_user you can move the session username setting stuff completly from the db.php and move it to a better place (e.g. session.php).
the error of you dont see the username if you require db.php is :
in your db.php first thing to do is to put the username in the session so when you call it from the page3 you the code put blank in the session
this code
$user = $_POST["user"];
$_SESSION['username'] = $user;
There is two solution for that :
1 - put connection in one file and the session put in the other file
$user = $_POST["user"];
$_SESSION['username'] = $user;
in different file of connection
2 - the second is you put if condition before this code like this
if(!empty($_POST["user"])) {
$user = $_POST["user"];
$_SESSION['username'] = $user;
}
try it .
here is my problem:
index.php contents:
require_once('phpcommonscripts/connections/connection.php');
require_once('phpcommonscripts/functions/logging.php');
Func_LogToDB($logType, $actionType, $errMsg, $varUser);
connection.php contents:
$hostname_MYDB = "localhost";
$database_MYDB = "MYDB";
$username_MYDB = "USER";
$password_MYDB = "PASS";
$MYDB = mysql_pconnect($hostname_MYDB, $username_MYDB, $password_MYDB) or trigger_error(mysql_error(), E_USER_ERROR);
logging.php contents:
function Func_LogToDB($lType, $lAction, $lMessage, $lUser) {
mysql_select_db($database_MYDB, $MYDB);
}
ERROR MESSAGE:
Notice: Undefined variable: database_MYDB in /home/notes/public_html/phpcommonscripts/functions/logging.php on line 22
( line 22 is the mysql_select_db() line... )
Am I doing something wrong here ?
Am I doing something wrong here ?
Yes you are :)
Those variables $database_MYDB, $MYDB aren't in same scope.
You cannot access variables which are defined outside a function from within a function.
I am using Jquery.load to load an external file with html/php content into one div. It loads the file, and displays what it's supposed to except it says access denied www-data#localhost password: no where it should be echoing some content.
I know that the main page, not the one being loaded, is connected to the db using require_once("assets/functions/config.php"); to call on my php file that contains the connection.
What am I doing wrong? It's probably simple, and I'm overlooking something.
EDIT: Okay on the index.php above <html> I have:
<?PHP
require_once("assets/functions/config.php");
//if ($notInstalled == 1) header("Location: install");
require_once("assets/functions/functions.php");
if ($users->checkAuth() == 0) {
header("Location: login.php");
exit;
}
$currentUser = $_COOKIE['vetelixir_username'];
?>
config.php is as follows:
<?
// MySQL Database
$db_host = "localhost";
$db_name = "dbname";
$db_username = "username";
$db_password = "password";
// Connect to the database
$connection = #mysql_connect($db_host,$db_username,$db_password) or die(mysql_error());
$db = #mysql_select_db($db_name,$connection)or die(mysql_error());
// end MySQL
?>
jQuery:
$("#button").click(function() {
$('#content').load('pages/external.php');
});
External File to Load:
<div id="div">
<?
$currentBlah = mysql_query("SELECT `firstname`,`lastname` FROM `blah` ORDER BY `lastname` ASC") or die(mysql_error());
while($u = mysql_fetch_array($currentBlah)) {
echo "<div class='clientRow'><span class='name'>".$u['firstname']." ".$u['lastname']."</span></div>";
}
?>
</div>
This looks like the php script that your jQuery function is calling is trying (and failing) to open an SSH session.
This question already has answers here:
Giving my function access to outside variable
(6 answers)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
Hello I am pretty sure they are in the same scope and am unsure why I am getting this error now as it was working before.
These are the other files in the same folder as
the php file :
dbh.php
recipeLookUp.php
It is line 9 that I am getting the error
this is the recipeLookUp.php
<?php
function lookup($sql,$column_name){
$results_search=array();
include_once 'dbh.php';
$results=mysqli_query($conn,$sql);
$resultsCheck = mysqli_num_rows($results);
if ($resultsCheck > 0) {
while ($row = mysqli_fetch_assoc($results)){
$info= $row[$column_name];
$results_search[]=$info;
}
return $results_search;
}
}
and here is my other file
<?php
$dbServername = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'Recipe';
$conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);
Im sure it probaly something small but I can't find it please help!!!
The include_once 'dbh.php'; within your function seems fishy. What are you gonna do if you have other functions which need to perform SQL queries as well?
Instead, I would put the include_once 'dbh.php'; outside (above) your function, and perhaps use require_once instead of include once. And then inside your function, put global $conn; above the mysqli_query call, so your $conn variable is defined there when you pass it along to mysqli_query.
However, if you are worried about creating unnecessary SQL connections (e.g. in case your lookup function is never actually called) I think it would be even better to put this in your dbh.php :
<?php
$conn = null;
function Query($sql)
{
global $conn;
if (!$conn) // will only connect if connection does not exist yet
{
$dbServername = 'localhost';
$dbUsername = 'root';
$dbPassword = 'xxxxxxxxxxx';
$dbName = 'Recipe';
$conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);
}
return mysqli_query($conn,$sql);
}
?>
And now in your main PHP file, or any PHP file where you include require_once 'dbh.php'; you can directly use Query($sql) to perform queries, which will take care of the connection itself (and only do so if necessary).