I am making a cms application and it was working fine until i made a new page called "session.php" and added a function into that page I then wrote the require once command into the main page of my application and when I run it says "Webpage is not available"...i remove the require once command and my application works again. Does anyone know how to fix this problem?
This is the code for session.php
<?php session_start(); function message() {
if (isset($_SESSION["message"])) {
$output = "<div class=\"message\">";
$output .= htmlentities($_SESSION["message"]);
$output .= "</div>";
return $output;
}}?>
This is the code for the page im trying to load
<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php include("../includes/layouts/header.php"); ?>
<?php find_selected_page(); ?>
<div id="main">
<div id="navigation">
<?php echo navigation($current_subject, $current_page); ?>
<br />
+ Add a subject
</div>
<div id="page">
<?php echo message(); ?>
<?php if ($current_subject) { ?>
<h2>Manage Subject</h2>
Menu name: <?php echo $current_subject["menu_name"]; ?>
<?php } elseif ($current_page) { ?>
<h2>Manage Page</h2>
Menu name:<?php echo $current_page["menu_name"]; ?>
<?php } else { ?>
Please select a subject or a page.
<?php }?>
</div>
</div>
<?php include("../includes/layouts/footer.php"); ?>
The code posted looks oddly-formatted, but I'll venture to say it's not a syntax error.
As others stated, you need to learn the "debugging" process which includes monitoring the error.log file (assuming Apache) and the browser's console (where applicable).
During development, it's a good idea to turn on error reporting to make this a bit easier. Place the following lines at the top of your PHP scripts:
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
That won't display 100% of the issues (99.8% give or take), so read up on how to view your server's error log. The answer to your question is hidden in there, I guarantee it.
Related
On the welcome page after the user has signed in, if I add this line of code <?php include_once('my_username_here.txt'); ?> and I replace “my_username_here.txt” with “john.txt”, it pulls what is saved on john.txt and shows it on the welcome page.
When I add this line of code <?php echo htmlspecialchars($_SESSION["username"]); ?> into this line of code like this <?php include_once(<?php echo htmlspecialchars($_SESSION["username"]); ?>.txt'); ?> it does not work. I have looked high and low and tried different ways and noting seems to work.
If I understand you correctly so all you need is to edit it like this
<?php
include_once(htmlspecialchars($_SESSION["username"]) . '.txt');
?>
or for simplification, you could edit it like this one
<?php
$file_name = htmlspecialchars($_SESSION["username"]) . '.txt';
include_once($file_name);
?>
I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>
I am new to PHP. I have these 3 files :
index.php
functions.php (to organize functions)
header.php
I want to simplify(which has been done so far) the index.php page thus I do not need to write the and all stuff again and again. So I created header.php that can be loaded by index.php:
header.php
<!Doctype html>
<html lang="en">
<head>
<title>Learn PHP</title> <!--This is the problem, every page that loads header.php will have same title! -->
</head>
<body>
<div class="header">
<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->
I have even simplified things further by making a function in functions.php so that I can just type "get_header()" in the index.php without writing the whole code again.
functions.php
<?php
function get_header(){
if (file_exists('header.php')){
require 'header.php';
}
else{
echo "There is an error retrieving a file";
}
}
?>
Now, how do I allow this index.php to have custom page title instead of the default given by header.php?
Am I missing something important. I have tried creating a variable and try to pass it to the functions.php, but it didn't work. Or is there any cleaner way to do this?
I am inspired by how wordpress organize their files, I have checked the wordpress file. And then I decided to try something from scratch so I understand better and improve my PHP skills.
I know can use POST and GET, but no I dont want to refresh or load a new page just to change a page title especially index.php
EDIT :
Here I included my index.php
<?php
require 'functions.php';
?>
<?php
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
It seems like all you need you want is simple includes. You're actually making it harder by using a function, here, because an include has the same scope as where it was included from. E.g.
header.inc
…
<title><?php echo isset($title) ? $title : 'Untitled';?></title>
…
index.php
<?php
$title = 'Welcome';
require 'header.inc';
?>
welcome
another-page.php
<?php
$title = '2nd page';
require 'header.inc';
?>
2nd page content
If you want to use a function, give it parameters.
function get_header($title = 'Some default title') {
…
}
the included file will have access to the variables in the function's scope.
in the functions.php
function get_header(){
if (file_exists('header.php'))
require 'header.php';
else echo "There is an error retrieving a file";
}
in the header.php, and in the balise title you call the session parameter
<!Doctype html>
<html lang="en">
<head>
<title>
<?php if(!empty($_SESSION['title-page'])) echo $_SESSION['title-page']; else 'Learn PHP'; ?>
</title>
</head>
<body>
<div class="header">
<h1>Learn PHP</h1>
<p>Your library of PHP learning!</p>
<hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->
and in the index.php
<?php
session_start();
$_SESSION['title-page'] = 'this is the welcome Page';
require 'functions.php';
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
and in another-page.php
<?php
session_start();
$_SESSION['title-page'] = 'this is an another Page';
require 'functions.php';
get_header();
?>
<table>
<h3>What we learned</h3>
<ul>
<li>Syntax </li>
<li>Variables </li>
<li>Code Flow </li>
<li>Arrays </li>
<li>Superglobals </li>
</ul>
</table>
<?php get_footer(); ?>
I have a series of php includes on a web page that look like this...
<?php include 'folder1/file.php'; ?>
<?php include 'folder2/file.php'; ?>
<?php include 'folder3/file.php'; ?>
They look like the above in my code editor but when I upload to my server it views like this when I look at the source code...
<?php include 'folder1/file.php'; ?>
<?php include 'folder3/file.php'; ?>
<?php include 'folder3/file.php'; ?>
So I thought maybe the file wasn't uploading....so I changed the code to this and uploaded the page....
<?php include 'folder1/file.php'; ?>
<?php include 'folder3/file.php'; ?>
And when viewing that in any browser it viewed as expected, showing just the two includes. Telling me that the web page is uploading properly to the server. I've emptied my cache, refreshed the page and nothing. So then when I change the code back to this and upload....
<?php include 'folder1/file.php'; ?>
<?php include 'folder2/file.php'; ?>
<?php include 'folder3/file.php'; ?>
It stills shows this in the live page
<?php include 'folder1/file.php'; ?>
<?php include 'folder3/file.php'; ?>
<?php include 'folder3/file.php'; ?>
I have PHP installed on the server and all other PHP codes/includes work correctly. The following PHP code does run correctly.
<?php phpinfo(); ?>
Here is the code for folder2/file.php
<div class="propertylistingbox" id="propertylistingbox">
<div class="proplistingleft" id="proplistingleft"><img src="/second-base/images/FirstBase01.png" alt="Second Base Exterior" width="284" height="193" border="0" />
<div class="reservation-request-button">
</div>
</div>
<div class="propertylistingmain" id="propertylistingmain">
<div class="propheader" id="propheader">
<h3>SECOND BASE</h3>
</div>
<div class="propbulletlist" id="propbulletlist">
<ul>
<li>Located In Oneonta, NY</li>
<li>Accommodates 4</li>
<li>High Speed Internet</li>
<li>2 Bedrooms</li>
<li>Close to 3 bedroom apartment "First Base"</li>
<li>1.25 Miles to Cooperstown Baseball World</li>
<li>2.75 Miles to Cooperstown All Star Village</li>
<li>19 Miles to Cooperstown Dreams Park</li>
</ul>
</div>
<div class="propdetails" id="propdetails">
<div class="details-button">
Click For More Details
</div>
</div>
<div class="propspecialsbanner" id="propdetails2"></div>
<div class="propcalendarmaplink" id="propcalendarlink"><strong>CALENDAR OF AVAILABILITY</strong><br />
<strong>GOOGLE MAP</strong></div>
</div>
</div>
I have a PHP page with HTML content in it. Now I run some PHP codes between HTMLs and I get some results. The fact is, when I try to get a respond from this page by AJAX it'll show me the whole page content and plus the result I was looking for. What can I do to prevent the page from writing extra content. I know whenever something get printed on page it'll go as respond to the AJAX call but I want a way to somehow fix this.
My page file (name: page.php):
<?php echo $Content->GetData('HEADER'); ?>
<div id="Content">
<div id="Page">
<?php if($Content->GetData('PAGE','IS_TRUE')) : ?>
<?php if(NULL !== $Content->GetData('PAGE','TITLE')) : ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeTitle"); ?>
<div id="Title" dir="rtl">
<?php echo $Content->GetData('PAGE','TITLE'); ?>
</div>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterTitle"); ?>
<?php endif; ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeContent"); ?>
<?php echo $Content->GetData('PAGE','CONTENT'); ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterContent"); ?>
</div>
</div>
<?php else : ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
There is no page like this in our archives.
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php echo $Content->GetData('FOOTER'); ?>
If I write an address in my browser like this localhost/cload/blog?action=rate it'll go through my redirection list and show the page.php with blog plugin loaded. The problem is I want to call my blog plugin by AJAX through this address but it will first render the page data.
Sorry if this is messy.
I'd suggest modifying page.php to be some functions, primarily a data processing function, and then an output function. Then, when you load the page, put a check in for whether it's an AJAX request, and if so, echo the data you want as JSON, otherwise render the page using the output function.
Alternatively, you could create a second, separate page, but that could be more difficult to maintain than a single file.
Yes, you should check whether your request is a ajax request, if it is so you should change your response to return only the result whatever want.
This php code will give an rough idea on this.
if($request->isXmlHttpRequest()){
return new Response(json_encode($data_array));
}
Hope this will help.