.shtml and .php pages Includes Virtual and Running PHP Problems - php

I have a strange issue going on. It may be server related, but I don't know what to look for.
I have a .php page with a few Virtual Includes:
<?php virtual ("nav.shtml"); ?>
...throughout it. I have also a parser that is displaying XML data in a table form.
The parser works with the standard:
<?php include ('parser.php'); ?>
...however, if I have the Virtual above the include, the parser doesn't work. Or at least it will not "find the file" however, the file is there and it works ABOVE the virtual, displaying it fine...
For example, this works:
<?php include ('parser.php'); ?>
<?php virtual ('file.shtml'); ?>
This doesn't:
<?php virtual ('file.shtml'); ?>
<?php include ('parser.php'); ?>
Am I missing something here?
Here is the index.shtml page code:
<?php virtual ("nav.shtml"); ?>
<div id="sortabletable">
<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>Subject</th>
<th>Committee</th>
<th>Witness</th>
<th>Date</th>
<th>Bill</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<?php include('parser.php'); ?>
</tbody>
</table>
</div>
<?php virtual ("footer.shtml"); ?>
Here is the parser code:
<?php
$xml_file = "test.xml";
$xml_subject_key = "*TESTIMONIES*CONGRESS*SUBJECT";
$xml_committee_key = "*TESTIMONIES*CONGRESS*COMMITTEE";
$xml_witness_key = "*TESTIMONIES*CONGRESS*WITNESS";
$xml_date_key = "*TESTIMONIES*CONGRESS*DATE";
$xml_bill_key = "*TESTIMONIES*CONGRESS*BILL";
$xml_link_key = "*TESTIMONIES*CONGRESS*LINK";
$congress_array = array();
$counter = 0;
class xml_story{
var $subject, $committee, $witness, $date, $bill, $link;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_subject_key, $xml_committee_key, $xml_witness_key, $xml_date_key, $xml_bill_key, $xml_link_key, $counter, $congress_array;
switch($current_tag){
case $xml_subject_key:
$congress_array[$counter]->subject = $data;
break;
case $xml_committee_key:
$congress_array[$counter]->committee = $data;
break;
case $xml_witness_key:
$congress_array[$counter]->witness = $data;
break;
case $xml_date_key:
$congress_array[$counter]->date = $data;
break;
case $xml_bill_key:
$congress_array[$counter]->bill = $data;
break;
case $xml_link_key:
$congress_array[$counter]->link = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
// A simple for loop that outputs our final data.
//echo sizeof($congress_array);
for($x=0; $x<count($congress_array); $x++){
echo "<tr><td scope='row'>" . $congress_array[$x]->subject . "</td>";
echo "<td>" . $congress_array[$x]->committee . "</td>";
echo "<td>" . $congress_array[$x]->witness . "</td>";
echo "<td>" . $congress_array[$x]->date . "</td>";
echo "<td>" . $congress_array[$x]->bill . "</td>";
echo '<td><img src="download-icon.png" width="20" height="20" alt="' . $congress_array[$x]->subject . '"/></td></tr>';
}
?>

"Will not find the file" ... which file? The XML ? The shtml?
Without seeing your code (cough) I can only guess that your .SHTML includes some executable PHP which is upsetting things ...
But without posted code all that we can do is guess

If you use Virtual and Includes in some cases you will need to split your includes code and run it at the top of your .shtml file. Then have the output includes where you need the output. Worked for my issue.
Thanks!

Related

failed to clone a script to a new server

i was trying to copy this script from one of my servers to another one
both of them are ubuntu 18 with php 5.6 on them
but i keep getting internal server error without changing anything
<?php
if(!isset($_POST['importSubmit'])){
header("Location: index.php");
}
date_default_timezone_set('UTC');
header('Content-Type: text/html; charset=utf-8');
$x = fopen($_FILES['file']['tmp_name'], 'r');
require('XLSXReader.php');
$xlsx = new XLSXReader($_FILES['file']['tmp_name']);
$sheetNames = $xlsx->getSheetNames();
foreach($sheetNames as $sheetName) {
$sheet = $xlsx->getSheet($sheetName);
array2Table($sheet->getData());
function array2Table($data) {
foreach($data as $row) {
echo "<tr>";
foreach($row as $cell) {
echo "<td>" . escape($cell) . "</td>";
}
echo "</tr>";
}
}
function escape($string) {
return htmlspecialchars($string, ENT_QUOTES);
}
?>
From what I can tell, you are using: https://github.com/gneustaetter/XLSXReader and this library is not installed in your new server. Install it, and you should probably be good.

How to call a function in PHP

I have been following some courses on how to create a session object, which has worked out fine, If I place the complete code onto a PHP file, all works great!
What I would like to do is place this in another module (PHP file) and just use one line (or equivalent) to do this, such as GetSessiondata();
<?php
$SqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");
$UserDataSet = $SqlCon->GetUserList("SELECT * FROM Users");
echo "<br /><br />";
echo "<br /><br />";
if ($UserDataSet)
{
echo "<table>" . "<thead>" ;
echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";
foreach($UserDataSet as $data)
{
echo "<td>" .$data->GetUsrName()."</td>" ;
echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;
}
echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
echo "</tbody>" . "</table>" ;
}
else
echo "Nothing Found in DB!";
?>
You need to "require" your file, where you want to use it.
Here an example
Working with classes:
Whatever.php
class Whatever {
public function __construct() {
// Ever when the code is instantiated, this will be called too!
}
public function myMethod() {
echo 'hello';
}
}
index.php
require_once('./Whatever.php');
$whatever = new Whatever();
$whatever->myMethod();
Without classes:
functions.php:
function whatever(){ echo 'hello'; }
index.php:
require_once('./functions.php');
whatever();
Read more:
Require: http://php.net/manual/es/function.require.php
Require_once: http://php.net/manual/es/function.require-once.php
My advise is to split this refactoring process into 2 steps:
1.Wrap your code into function:
function someFunctionName() {
$SqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");
$UserDataSet = $SqlCon->GetUserList("SELECT * FROM Users");
echo "<br /><br />";
echo "<br /><br />";
if ($UserDataSet)
{
echo "<table>" . "<thead>" ;
echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";
foreach($UserDataSet as $data)
{
echo "<td>" .$data->GetUsrName()."</td>" ;
echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;
}
echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
echo "</tbody>" . "</table>" ;
}
else
echo "Nothing Found in DB!";
}
// and call your function
someFunctionName();
2.Create another file, let's say functions.php, in the same dir and move function into it. Now you can require this file inside your php page:
require_once 'functions.php';
// and call your function
someFunctionName();
I think you are looking for include
Save your file, and then you can include it in another file as:
include 'my_file.php';
You can also use:
include_once
require
require_once
Read the documentation for further explanations or see this question

Replacing characters with smileys from echoed row

I can't figure this out and isn't my strong side of codeing ether.
As of right now it'll only print the first person and timestamp, not anything more.
<table cellpadding="0" cellspacing="0" width="100%">
<tr><td></td></tr>
<?php
include '../connection.php';
$sql = "SELECT *
FROM messagebox
INNER JOIN person
ON messagebox.sid = person.sid
ORDER BY messagebox.id DESC
LIMIT 20
";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
if ($switch=='1')
{
echo "<tr bgcolor=\"#FFFFFF\">";
$switch='0';
}
else
{
echo "<tr bgcolor=\"#F9F9F9\">";
$switch='1';
}
$elfstring = utf8_encode($row['shout']);
function smiley($elfstring) {
$elfstring = ereg_replace(":)","<img src=!.png alt=\"!\" >", $elfstring);
$elfstring = ereg_replace(":(","<img src=laugh.gif alt=\":D\" >", $elfstring);
$elfstring = ereg_replace(":p","<img src=tongue.gif alt=\":p\" >", $elfstring);
return $elfstrings;
}
$messages = smiley($elfstring);
echo "";
echo "<td width=\"100\" valign=\"top\"><strong>" . $row['name'] . "</strong></td>";
echo "<td width=\"100\" valign=\"top\">" . "(" . $row['place'] .")</td>";
echo "<td width=\"70\" valign=\"top\">" . "" . date('H:i:s',strtotime ($row['timestamp'])) ."</td>";
echo "<td valign=\"top\">" . smiley($elfstrings) . "</td>";
echo "</tr>";
}
?>
<tr>
<td>
</td>
</tr>
</table>
I know some parts of this code is deprecated, but the server using this is old and isn't up to date.
Thanks in advance for help.
You can't declare a function multiple times. Move the function smiley(){ outside of your while loop.
You should enable error reporting and monitor your error logs.
You should indent each control block so you can easily tell where blocks end/start.
If you don't need a regex/ aren't using one don't use a regex function. The ( and ) are special regex characters and will cause errors. Use str_replace because you are doing static replacements anyway.
You can enclose strings in " or ', this can simplify string construction because you won't need to escape.
So make the ending of your script:
function smiley($elfstring) {
return str_replace(array(':)', ':(', ':p'),
array('<img src="!.png" alt="!" >', '<img src="laugh.gif" alt=":D" >', '<img src="tongue.gif" alt=":p" >'),
$elfstring);
}

My PHP array somehow doesn't see my array as an array

Okay, I've narrowed this down to one key function.
It seems impossible, but every time I've "echo"ed it, this function always says that the variable array I'm using to store my data is no longer an array.
Offending Code
private function do_display_message_details()
{
$m_message_values = '';
$m_address = APP_ROOT_PATH;
if ($this->c_arr_stored_message_data)
{
echo "I AM AN ARRAY";
}
else
{
echo "I AM NOT AN ARRAY";
}
$m_message_name = $this->c_arr_stored_message_data['message-name'];
$m_arr_stored_message_data = $this->c_arr_stored_message_data['message-retrieved-data'];
foreach((array)$m_arr_stored_message_data as $m_message_value)
{
$m_message_row = explode(',', $m_message_value);
$m_message_values .= '<tr>';
$m_message_values .= '<td>' . $m_message_row[0] . '</td>';
$m_message_values .= '<td>' . $m_message_row[1] . '</td>';
$m_message_values .= '<td>' . $m_message_row[2] . '</td>';
$m_message_values .= '</tr>';
}
$this->c_html_page_content = <<< VIEWSTOREDMESSAGEDATA
<div id="lg-form-container">
<h2>Message name: $m_message_name</h3>
<h3>Message Data</h3>
<table border="1">
<tbody>
<tr>
<th>Date</th>
<th>Time</th>
<th>Message Values</th>
</tr>
$m_message_values
</tbody>
</table>
<br />
<form method="post" action="$m_address">
<label for="anothergo">Another Message?</label>
<button name="feature" value="display_message_data">Review Stored Message Data</button>
</form>
</div>
VIEWSTOREDMESSAGEDATA;
}
Constructor to show you it is set up as an array
private $c_arr_stored_message_data;
private $c_error_message;
private $c_page_content;
// ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
public function __construct()
{
$this->c_arr_stored_message_data = array();
$this->c_error_message = '';
$this->c_page_content = '';
}
But, if you try it on a different function, it works.
Trial Code That Works!
// ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
public function set_stored_message_data($p_arr_stored_message_data)
{
$this->c_arr_stored_message_data = $p_arr_stored_message_data;
if ($this->c_arr_stored_message_data)
{
echo "I AM AN ARRAY";
}
else
{
echo "I AM NOT AN ARRAY";
}
}
Your "I AM AN ARRAY" checks don't work quite as you think:
$x = array();
if ($x) {
echo 'I AM AN ARRAY';
} else {
echo 'I AM NOT AN ARRAY';
}
That prints out 'I AM NOT AN ARRAY', as an empty array evaluates to false in PHP. You can use var_dump to see exactly what content your variables have.
You have a variable named $c_page_content but later you refer to $c_html_page_content. Perhaps a mistake?
If the "messages" can contain user-supplied text, then there are XSS vulnerabilities in your code. It is essential to use htmlspecialchars to render text in HTML. For example, if $m_message_row[0] is text, you must write '<td>' . htmlspecialchars($m_message_row[0]) . '</td>'. Ditto for any other values that are not supposed to contain HTML markup that are written to HTML.
Your main issue, that the data is apparently not being stored in your c_arr_stored_message_data variable, is not possible to reproduce without seeing the connecting code that calls set_stored_message_data and do_display_message_details. All I can suggest is that you make sure you're calling them in the right order on the same instance of the same class.

php outputs part of code without any echo

Here's my piece of code(full body code):
<body>
<script type='text/javascript'>
function AddEvent(Syear, Smonth, Sday, Eyear, Emonth, Eday, hallNumber){
...
}
</script>
<?php
function GetMonthByCoding($first , $second , $third) {
...
}
function GetDateByCoding($coding){
...
}
function GetDateFromLine($line){
...
}
$userid = '...';
$magicCookie = 'cookie';
$feedURL = "...";
$sxml = simplexml_load_file($feedURL);
foreach ($sxml->entry as $entry) {
$title = stripslashes($entry->title);
if ($title == "HALL") {
$summary = stripslashes($entry->summary);
$date = GetDateFromLine($summary);
echo ("<script type='text/javascript' language='JavaScript'> AddEvent(" . $date['start']['year'] . ", " . $date['start']['month'] . ", " . $date['start']['day'] . ", " . $date['end']['year'] . ", " . $date['end']['month'] . ", " . $date['end']['day'] . "); </script>");
}
}
?>
</body>
AddEvent() is JavaScript function defined earlier.
What I get in my browser is:
entry as $entry) { $title = stripslashes($entry->title); if ($title == "HALL") { $summary = stripslashes($entry->summary); $date = GetDateFromLine($summary); echo (""); } } ?>
Looks like it was an echo but as you can see there is no echo right in the middle of foreach.
Can anyone say what I am doing wrong?
PHP is not installed, or it is not enabled, or the file is not a .php file or the server has not been told to recognise it as a file to parse.
Try View Source and you should see all your PHP code. The only reason part of it shows up is because everything from <?php to the first > is considered by the browser to be an invalid tag.
I found the problem, it was in the name of variable sxml. I renamed it and the problem escaped.

Categories