This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 7 years ago.
config.php ( edited but still have same problems ):
<?
$judul ="my site title";
?>
my index.php:
<?php
session_start();
include('config.php');
mysql_connect($server,$login,$pass) or die("Nggak bisa koneksi");
mysql_select_db($db);
$fail = " ";
?>
<title><? echo "$judul"; ?></title>
</head>
the result is my website title is
<? echo "judul; ?>
not like i want to...
Is there anything that I should change?
the goal is:
I want to put some variable like footer copyright title inside config.php so if I need to edit it's only need to edit config.php
----edited----
by the way thank you for all answer
it is work now
i use #Tanuel comment
once again thankyou
i vote answer ( green checklist ) based on the first answer, since i use syntax from comment
config.php
define('title','Hello Word');
index.php
include('config.php')
`<title><?php echo title; ?></title>`
First, you have a mistake in your config.php. You forgot $. Second, you $judul is not declared in your index.php. May be you must change it to $title after correcting config.php?
In config.php, the variable declaration is incorrect. Use the below code for that.
<?php
$title = "Site Title";
?>
If your idea was to define the title as a constant, use the below code instead (on config.php)
<?php
define('title',"Site Title");
?>
In the index.php, incorrect variable is used. Use $title to get the actual title initialized in config.php (if you are using variable to get title). Use the below code on index.php
<?php
session_start();
include('config.php');
mysql_connect($server,$login,$pass) or die("Nggak bisa koneksi");
mysql_select_db($db);
$fail = " ";
?>
<head>
<title><?php echo $title; ?></title>
</head>
If you want to use the constant instead, use the below code
<?php
session_start();
include('config.php');
mysql_connect($server,$login,$pass) or die("Nggak bisa koneksi");
mysql_select_db($db);
$fail = " ";
?>
<head>
<title><?php echo title; ?></title>
</head>
Define the variable config.php as global variable.
<?php
global $pageTitle;
$pageTitle = "Site Title";
?>
And in the index.php again you have to call it as global variable.
<?php
session_start();
include('config.php');
mysql_connect($server,$login,$pass) or die("Nggak bisa koneksi");
mysql_select_db($db);
$fail = " ";
global $pageTitle;
?>
<head>
<title><?php echo $pageTitle; ?></title>
</head>
If you want in multiple pages just use it like array.
In config.php
<?php
global $pageTitle;
$pageTitle['page1'] = "Site Title 1";
$pageTitle['page2'] = "Site Title 2";
?>
Use this code
config.php
<?php
define('title','my site title');
?>
my index.php
<?php
session_start();
include('config.php');
mysql_connect($server,$login,$pass) or die("Nggak bisa koneksi");
mysql_select_db($db);
$fail = " ";
?>
<title><?php echo title; ?></title>
</head>
Related
I was studying sessions and came up with a problem. On my first page, index.php, I wrote a simple code to start a session, given as follow:
<?php
session_start();
?>
<html>
<body>
<?php
$_SESSION['name'] = 'Daniel';
echo $_SESSION['name'];
?>
<ul>
<li>index</li>
<li>contact</li>
</ul>
</body>
</html>
Next, I created a file contact.php as follow:
<?php
session_start();
?>
<html>
<body>
<?php
echo $_SESSION['name'];
?>
This is contact page.
</body>
</html>
In contact.php the session variable $_SESSION['name']; is not displayed. But in the first file, index.php, the session variable is set and is being displayed.
What's wrong here? Why isn't the session variable in contact.php being displayed?
Consider the code
<?php
.....
.....
$error="abc";
......
?>
<html>
<head></head>
<body>
.....
<?php echo $error ?>
.....
</body>
</html>
I am new to php. I want to access the same "error" variable at two parts in the same file. Is there any way to do so? Or I have to create another file with the "error" variable and then include it in the file where I need it again?
You should be able to access the variable as many time as you need it if it's part of the same scope.
This will work:
<?php $foo = 'bar' ?>
<hr />
<?php echo $foo; ?>
This will not:
<?php
function set_foo_variable() {
$foo = 'bar';
}
set_foo_variable();
?>
<hr />
<?php echo $foo; ?>
Make sure your variable is always in the same scope AND is set.
Here's more documentation on PHP scope: http://php.net/manual/en/language.variables.scope.php
Have you already tried accessing it?
You shouldn't have an issue doing something like the following:
<?php $error="abc"; ?>
<html>
<head>
</head>
<body>
<?php echo $error; ?>
</body>
<?php echo $error; // access #2 ?>
</html>
<?php echo $error; // access #3 ?>
Note:
For the future, I would really try to improve the code format of your questions, mention what you tried to do already and provide more details about your issue.
So I am trying to practice PHP and I am stuck with using headers. I am using xampp in doing this. I have a simple form wherein the user will log in in the "index.php" now when the log in is successful the header function will start and redirect the page to "includes/profile.php". Now here is the problem, after the header I am currently in "includes" folder so when I use other .php or .css files outside includes folder i need to do "../example/example.php". This mess up my paths because my CSS file is in "css/example.css" so i need to put "../". Is there any way to always make the "pointer" go back to the parent directory even after using header so that i dont need to use "../"?
index.php
<?php
session_start();
if(isset($_SESSION['username'])&& isset($_SESSION['password']))
{header("Location: includes/profile.php");exit;}
if (isset($_POST['submit'])) {
if($_POST['username']=="junji" && $_POST['password']=="secret"){
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['expiry']=time();
$username = $_SESSION['username'];
$password = $_SESSION['password'];
header("Location:includes/profile.php");
exit;
}
else{
$username = "";
$password = "";
session_destroy();
}
}
?>
//end of index.php
now inside the profile.php
<?php
session_start();
if(isset($_SESSION['username'])&& isset($_SESSION['password']))
{if(time()-$_SESSION['expiry']<5){?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include_once('head.php');
?>
</head>
<body>
<div class="container">
<h1>Junji's Bio</h1>
<div class="row">
<div class="col-xs-8">
<h2>Content</h2>
<?php
include_once ('content.php');?>
</div>
<form method="POST">
<div class="col-xs-4 sidebar">
<div class="alert alert-success"> <?php print "You are currenly logged in as ";
print '<br><h3>';
print $_SESSION['username'];print "\t".'<input type="submit" class="btn btn-info" name="submit" value="Logout">'; ?></h3> </div>
</div></form>
</div>
</div>
</body>
</html>
<?php if(isset($_POST['submit'])){
session_destroy();
header("Location: ../index.php?msg=You have been successfully logged out!");
exit;
}
}else
{session_destroy();
header("Location:../index.php?msg2=Your session has expired! Please Log-in again.");
exit;
}
}
else
{session_destroy();
header("Location:../index.php");exit;
}
as you can see the includes are directly called and no "includes/example.php" is needed. which means inside my "head.php" i need to make two
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"> just so i can get the css to work on both "index.php" and "includes/profile.php"
If you don't mind using an absolute path,
Try using "http://localhost/css/example.css"
The way I handle this is to set a level variable at the top of the page:
$level = ''; This would be top level
$level = '../' This would be for files in a top level folder
$level - '../../'; This would be for files in a sub directory... and so on
Then all you need to do is set the $level var in the path:
$level.'css/example.css -- works everytime
NOTE: If you are using .htaccess you need to call the full url to both .css and .js files
You could define a constant containing the absolute root, and include that in each path. If you save your root in a variable or constant, instead of write it directly in your files, it will be easier for you if your root changes in the future.
define("ROOT", "http://localhost/");
include ROOT . 'example/example.php';
Or maybe construct a function to call?
function path($string, $echo = TRUE) {
$root = "http://localhost/";
if($echo === TRUE) {
echo $root . $string;
} else {
return $root . $string;
}
}
path('index.php');
header("Location: " . path('test.php', false));
Can some one tell me how to "include" a variable from another .php file without all its other content.
index.php
<?php
$info=file('somedir/somefile.php');
$v1=trim($info[2]);
$v2=trim($info[3]);
$v3=trim($info[4]);
?>
the somedir/somefile.php
<?php
$variable=something;
$variable2=someotherting;
$variable3=thirdone!;
All the other content there may not be runned or showed.
?>
Can anybody please help me??
Edit:
Its for my dynamic page.
<html>
<?php
include_once 'config.php';
include_once 'includes/mysqlconnect.php';
$url_slash=$_SERVER['REQUEST_URI'];
$url= rtrim($url_slash, '/');
//$url = basename($url);
$info=file('sites/'.$url.'.php');
$title=trim($info[2]);
?>
<head>
<meta charset="UTF-8">
<title>$title</title>
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/reset.css">
<link rel="stylesheet" type="text/css" href="<?php echo $domain;?>themes/<?php echo $theme;?>.css">
</head>
<body class="body">
<div class="container-all">
<?php include_once 'includes/header.php';?>
<div class="container">
<?php include_once 'includes/navigationbar.php';?>
<?php include_once 'includes/rightsidebar.php';?>
<div class="content"><?php
if ($url==''){
include_once "sites/home.php";
}
elseif (file_exists("sites/$url.php") && is_readable('/var/www/html/sites/'.$url.'.php')){
include_once '/var/www/html/sites/'.$url.'.php';
}
else {
include_once 'sites/404.php';
}
?></div>
<?php include_once 'includes/footer.php';?>
</div>
</div>
</body>
</html>
Hope you understand my question now.
Programming is just driving your thoughts :)
So what i want to say that your question is how you can include just some part of an included file and my answer is that you can achieve that by doing a test each time the main file is included from withing this file to see if the file is included internally or not and you can be more precise in a way that you split your main file into block which are loaded due suitable variable
Take a look for this workaround and hope you will understand what i mean
Supposing we have the main file named main.php contains that contents
<?php
echo 'I am a java programmer';
echo 'I know also PHP very well';
echo 'When the jquery is my preferred toast !';
?>
now i have three external files that will include that file each file is specific for one of this 3 programming language
So i will create my 3 files in this way :
File : java.php
<?php
$iamjavadevelopper = 1;
include_once("main.php");
?>
File : phpfav.php
<?php
$iamphpdevelopper = 1;
include_once("main.php");
?>
File : jquery.php
<?php
$iamjquerydevelopper = 1;
include_once("main.php");
?>
and my main.php will be coded in this way
<?php
if(isset($iamjavadevelopper))
echo 'I am a java programmer';
if(isset($iamphpdevelopper))
echo 'I know also PHP very well';
if(isset($iamjquerydevelopper))
echo 'When the jquery is my preferred toast !';
?>
By this way each one of our three external files will show just a part of the included file :)
The only way I can think of without cookies or session's is to make an if condition in the page.
like that:
index.php
<?php include('somedir/somefile.php');?>
the somedir/somefile.php
<?php
if ($pageName != 'somefile.php') {
$variable=something;
$variable2=someotherting;
$variable3=thirdone!;
} else {
// All the other content
}
?>
Save the variables in a separate file that can be included separately. Do it the sane way. Structure your code properly, don't try to invent solutions for problems you have because your structure is messy.
I am a beginner and trying to use PHP statement inside <a> tag of HTML. I don't know whether it is possible or not and I tried to search it on Google, but I couldn't find any answer. Below is the code I am trying to execute.
Whenever I run this code, I do not get any error but the my browser does not display the value of $link1, $link2 and $link3 which I put inside the <a> tag of HTML.
I saved the document as index.php
<?php
$title = 'Shellitic';
$link1 = 'Home';
$link2 = 'Contact';
$link3 = 'About';
?>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1> Welcome to <?php echo $title; ?> </h1>
<p></br></p>
<p>To visit our home page, click on the <?php $link1; ?> button</p>
<p>To visit our contact page, click on the <?php $link2; ?> button</p>
<p>To visit our About page, click on the <?php $link3; ?> button</p>
</body>
You need to echo the values:-
<?php echo $link1; ?>
Some servers may accept this
<?= $link1; ?>
But it is typically safer to use
<?php echo $link1; ?>
Add echo command.
Like this:
<?php echo $link1; ?>