Require/Include in php [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I need some help, I have 2 php files and when I am calling some functions it tells me that
Fatal error: Call to undefined function get_contents() in /home/f77inq/public_html/php/shares.php on line 13
eventhough in the files from where I call the functions I have typed require("name ofmy file");
shares.php
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Shares</title>
<link rel='stylesheet' type='text/css' href='style.css' />
</head>
<body>
adad
<?php
$data = get_contents();
print_contents($data);
?>
</body>
</html>
This is the file from where I am calling the functions.
<?php
require("../../php_include/services.php");
require("../../php_include/query.php");
putenv("ORACLE_HOME=/oracle/product/12.1.0/dbhome_1");
function get_contents() {
$conn = db_connect();
$shares = get_share($conn);
db_disconnect($conn);
$contents = array('shares' => $shares);
return $contents;
}
function print_contents($contents) {
?>
<table>
<tr>
<th>Company Name</th>
<th>Rate</th>
</tr>
<?php
foreach ($contents['shares'] as $share) {
print "<tr>";
//****** LINKS TO EVERY
$identifier = urlencode($share['COMPANY']);
print "<td><a href='share-details.php?company={$identifier}'>{$share['COMPANY']}</a></td>";
print "<td>{$share['RATE']}</td>";
print "</tr>";
}
?>
</table>
<?php
}
require ("shares.php"); //<<<<<<<<<<<<<<<<<<<<<<
?>
As you can see down bellow I require the shares php file.
Both files are located in the same folder

for testing i put this code in a file named like.php-
<?php
function get_contents() {
echo "Got it";
}
require ("shares.php");
?>
and in the shares.php file i used this-
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>Shares</title>
<link rel='stylesheet' type='text/css' href='style.css' />
</head>
<body>
<?php
$data = get_contents();
echo $data;
?>
</body>
</html>
it works perfectly without eny error.

The error you show is from a file shares.php, yet the file you require is share.php. This error is likely happening somewhere else in the process since all of the code you've shown is valid and certainly works.

Related

I am not able to set session after login [duplicate]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am not good at this and have been trying but its not working. I don't know why the get.php is index page and its file header.php has another file login.php. I don't know why the session is not working the way I think it should. Footer should show username when logged in but it doesn't.
get.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
<body>
<?php include("header.php");?>
<?php include("menu.php");?>
<?php include("slider.php");?>
<?php include("content.php");?>
<div id="footer">
<?php
if(isset($_SESSION['currentuser'])==true)
{
echo"$username";
}
else
{
echo" not logged in ";
}
?>
</div>
</body>
</html>
login.php
<?php
session_start();
include("connect.php");
if(isset($_POST['login']))
{
$username=$_POST['username'];
$password=$_POST['password'];
$query="select * from user where username='$username' AND password='$password'";
$run=mysql_query($query);
if(mysql_num_rows($run)>0)
{
$_SESSION['currentuser']=true;
header("Location:get.php");
}
else
{
header("Location:get.php#loginfail");
}
}
?>
Try this:
if(isset($_SESSION['currentuser']) && $_SESSION['currentuser'] == true)
Also remember to include session_start() on each page which requires sessions.
Sessions can only be accessed after a page refresh when they are set. You'll need to reload the page to be able to access the session variables.
Another note: Everywhere you want you use any kind of session, you'll need to start the session with:
session_start();
So your get.php file would look something like this:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
.....etc

php session not working for popup login [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am not good at this and have been trying but its not working. I don't know why the get.php is index page and its file header.php has another file login.php. I don't know why the session is not working the way I think it should. Footer should show username when logged in but it doesn't.
get.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
<body>
<?php include("header.php");?>
<?php include("menu.php");?>
<?php include("slider.php");?>
<?php include("content.php");?>
<div id="footer">
<?php
if(isset($_SESSION['currentuser'])==true)
{
echo"$username";
}
else
{
echo" not logged in ";
}
?>
</div>
</body>
</html>
login.php
<?php
session_start();
include("connect.php");
if(isset($_POST['login']))
{
$username=$_POST['username'];
$password=$_POST['password'];
$query="select * from user where username='$username' AND password='$password'";
$run=mysql_query($query);
if(mysql_num_rows($run)>0)
{
$_SESSION['currentuser']=true;
header("Location:get.php");
}
else
{
header("Location:get.php#loginfail");
}
}
?>
Try this:
if(isset($_SESSION['currentuser']) && $_SESSION['currentuser'] == true)
Also remember to include session_start() on each page which requires sessions.
Sessions can only be accessed after a page refresh when they are set. You'll need to reload the page to be able to access the session variables.
Another note: Everywhere you want you use any kind of session, you'll need to start the session with:
session_start();
So your get.php file would look something like this:
<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" media="all">
</head>
.....etc

tables are not aligning to the top of the page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
my code:
achieved with the following code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo '<table><tr>';
$folders = #scandir('Users');
foreach($folders as $item):
if ((substr($item, 0, 1) == '.') || (preg_match("/\.php$/", $item)))
continue;
?>
<td>
<table width="220" border="1" valign="top">
<tr><th width="210" valign="top"><?php echo $item;?></th></tr>
<?php
if (is_dir("Users/$item"))
$target_folders = #scandir("Users/$item/uploaded/");
foreach($target_folders as $target_item){
if ((!preg_match("/^[.]/",$target_item)) || (!is_dir("Users/$item/uploaded/$target_item"))){
if ((substr($target_item, 0, 1) == '.'))
continue;
?><tr><td><?php echo $target_item ;?></td></tr><?php
}
}
?>
</table>
</td>
<?php
endforeach;
echo '</tr></table>';
?>
</body>
</html>
Now for the life of me i cant get the data that is displayed to align to the top of the page. i really don't know what i am doing wrong here but i do know that it is probably something simple.
a link to what i am displaying :
Current Code Outcome
add follownig in style sheet
td {
vertical-align: top;
}
regards
try
align td as top instead of table valign - top
posting since I wrote it anyway and then you changed the question content
present a jfiddle.net with actual rendered html,
explain why you are using such a complex doctype
try
http://jsfiddle.net/mplungjan/kG7B4/
td { vertical-align:top }

PHP index.php including structure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need help. On my site I use URL parameters to decide which other PHP files to include on the page. My first question is: What should be in index.php and what should be in the included PHP file?
On the Internet I found instructions, suggesting this structure for index.php:
<html>
<head>
<?php include 'header.php'; ?>
</head>
<body>
<?php include 'menu.php'; ?>
<?php include 'content.php'; /* Including page based on the parameters in the url */ ?>
</body>
</html>
With this structure, how can I change the data in the <head> section based on the content in content.php? For example, for index.php?option=article&id_article=1, I will include article.php and show the article with id 1. How, then, can I change the <title>, <meta>, etc content when <head> was written before including the article?
Thanks!
One option that is kind of ugly but will work is instead of having header.php echo have it simply set variables like $title and $meta[]. Also instead of having article.php from echoing return a variable like $html. Also in article.php you can then overwrite any of the variables set in header.php. Then you can construct your index.php like so:
<?php include 'header.php'; ?>
<?php include 'article.php'; ?>
<html>
<head>
<?php echo $title ?>
</ head>
<body>
<?php include 'menu.php'; ?>
<?php echo $html ?>
</ body>
</ html>
Or you can look into ob_start() and ob_flush() etc...
To be as simple as possible, you could make your header a function, then call the function elsewhere...
Example (untested):
function drawHeader($title, $desc = "", $keywords = "", $extra = "") {
?>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $desc; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">
<link rel="stylesheet" type="text/css" href="path/to/my.css">
<?php echo $extra; ?>
</head>
<?php
}
The goal of the above would be so you could easily and quickly do something like...
<!DOCTYPE html>
<html>
<?php drawHeader("Home", "Some description", "Some, keywords, here"); ?>
<body>
<h1>Hello, world</h1>
<?php drawFooter(); // maybe a footer of some type? ?>
</body>
</html>
Or you could set the variables before calling the included file... and in the included file simply echo those values in the proper places.
There's tons of ways to do this, lots of standards and best practices, frameworks, templating systems, placeholders using output buffering, etc.
first the instructions you found have nothing to learn from
as second to get the content of the file article.php
using the url index.php?option=article&id_article=1
you will need to use the $_GET['id_article']
Example :
$page = $_GET {'id_article'};
if (isset($page)) {
$url = $page.".php";
if (file_exists($url)) {
include $url;
}
and you can use database for storing the articles and use the query then using
if ($_REQUEST['id_article'] == $page) {
$querythearticle = mysql_query("select tablename from database_name where id_article='$page'");
}

How to pass arguments to an included file?

I'm trying to make the whole <head> section its own include file. One drawback is the title and description and keyword will be the same; I can't figure out how to pass arguments to the include file.
So here is the code:
index.php
<?php include("header.php?header=aaaaaaaaaaaaaaaaaaaaa"); ?>
<body>
.....
..
.
header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php $_GET["header"]?> " >
<meta name="Description" content=" <?php $_GET["header"]?> " >
<title> <?php $_GET["header"]?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
Obviously this doesn't work; how can I pass arguments to an included file?
Include has the scope of the line it's called from.
If you don't want to create new global variables, you can wrap include() with a function:
function includeHeader($title) {
include("inc/header.php");
}
$title will be defined in the included code whenever you call includeHeader with a value, for example includeHeader('My Fancy Title').
If you want to pass more than one variable you can always pass an array instead of a string.
Let's create a generic function:
function includeFile($file, $variables) {
include($file);
}
Voila!
Using extract makes it even neater:
function includeFileWithVariables($fileName, $variables) {
extract($variables);
include($fileName);
}
Now you can do:
includeFileWithVariables("header.php", array(
'keywords'=> "Potato, Tomato, Toothpaste",
'title'=> "Hello World"
));
Knowing that it will cause variables $keywords and $title to be defined in the scope of the included code.
index.php:
<?php
$my_header = 'aaaaaaaaaaaaaaaaaaaa';
include 'header.php';
?>
and header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php echo $my_header ?> " />
<meta name="Description" content=" <?php echo $my_header ?> " />
<title> <?php echo $my_header ?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
It's not an ideal solution, but I understand it's your first steps in php.
PS. Your Doctype doesn't match the code. I've adjusted your header html to be XHTML.
You can't pass arguments to include, but it has access to all variables you've already set. From the include documentation:
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.
Thus:
index.php
<?php
$header = 'aaaaaaaaaaaaaaaaaaaaa';
include("header.php");
?>
header.php
<title> <?php echo $header; ?> </title>
Well marc, when you are using include, you can simply just set up a variable to use:
<?php
$var = "Testing";
include("header.php");
?>
In your header file:
<?php
echo $var;
?>
Allow your previously defined variables are usable in any include you have.
you are over thinking it
<?php
$header = "aaaaaaaaaaaaaaaaa";
include("header.php");
?>
::EDIT::
Decided I would add value
The included file will gain the scope of where you included it. So if you include a file INSIDE a function:
<?php
$get_me = "yes";
function haha()
{
include("file.php");
}
haha();
// And file.php looks like
echo $get_me; // notice + blank
?>
More over, you include the same file more than once to great effect.
<?php
$output = "this";
include("cool_box.php");
$output = "will";
include("cool_box.php");
$output = "work";
include("cool_box.php");
?>
And even use this to load templates that become part of a method in a class. So you can do something like:
<?php
class template
{
private $name;
function __construct($name)
{
$this->name = preg_replace("/[^a-zA-Z0-9]/", "", $name);
}
function output(array $vars)
{
include($this->name.".php"); // Where $vars is an expected array of possible data
}
}
$head = new template("header");
$body = new template("body");
$head->output();
$head->output(array("content" => "this is a cool page"));
?>
defining a variable as a pseudo-argument/workaround before an include() - as recommended by many - is a bad idea. it introduces a variable in the global scope. define a function in the included file instead to catch the arguments u want to pass.
This is good approach. I however would do it a bit inside out. Define a layout, a wrapper for your webpage and include your content file into it:
layout.phtml
<html>
<head>
... your headers go here
</head>
<body>
<? include $content ?>
</body>
</html>
Your content template file can look like this e.g.
content.phtml
<h1>hello world</h1>
<p>My name is <?= $name ?></p>
Then, you would have your main script (index) that will handle logic, connects to database etc.
index.php
$content = 'content.phtml';
$name = 'Marc'; //Can be pulled from database
include 'layout.phtml';
This way, you can nicely separate business logic and presentation. And it can help you cut repetitive code for parts of page like logo or navigation which are repeated on the whole site.
If you include a file it is just like inserting that code into the parent file. You could simply do this:
<?php
$parameter = "Hello World";
include("header.php");
?>
and then in the header.php
<?php
$parameter = isset($parameter) ? $parameter : "Default Text";
// Use accordingly
?>
I used the isset() method to verify that it has a value already and is instantiated.
I noticed nobody suggested using a template engine. I came looking here because for the project I'm working with, a template engine isn't possible and that might be your situation too, however I thought it might be worth mentioning these: Twig (my preferred engine) and Smarty both allow passing specific variables to includes.
I highly recommend the use of a template engine whenever possible, as it simplifies your front end code, adds a layer of abstraction between your front end and back end, and both Twig and Smarty automatically clean the variables you pass to them which helps mitigate XSS attacks.
Twig Example
header.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content="{{ header }}" >
<meta name="Description" content="{{ header }}" >
<title> {{ header }} </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
index.html
{% include 'header.html' with { 'header' : '<script>alert("this shouldnt work")</script>'} only %}
Body Text
{% include 'footer.html' %}
Smarty Example
header.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content="{$header}" >
<meta name="Description" content="{$header}" >
<title> {$header} </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
index.tpl
{include 'header.tpl' header='<script>alert("this shouldnt work")</script>'}
Body Text
{include 'footer.tpl'}

Categories