Hyperlinks do not get proper value - php

I have a PHP script that has an infinite loop in it. The browser does not print the first echo statements in the loop, it does not even provide proper links to the hyperlinks. What could have gone wrong?
Demo
Code:
<html>
<head>
<style>
img{
height:200px;
width:200px;
}
</style>
</head>
<body>
<?php
error_reporting (0);
set_time_limit(0);// Setting an infinite timeout limit.
for($i=1; ;$i++)
{
$to_send = "http://graph.facebook.com/";
$to_send_new = $to_send.$i;
$content = file_get_contents($to_send_new);
$parsed = json_decode($content);
$link = $parsed->link;
$link = str_replace("http://www.facebook.com","https://graph.facebook.com",$link);
$link .="/picture?width=200&height=200";
if(!$parsed->first_name)
goto a;
?>
<br>
First Name: <?php echo $parsed->first_name;?>
<br>
Last Name:<?php echo $parsed->last_name;?>
<br>
Full Name :<?php echo $parsed->name;?>
<br>
Facebook Username:<?php echo $parsed->username;?>
<br>
Gender:<?php echo $parsed->gender;?>
<br>
Profile Picture
<!--
<img src="<?php/* echo $link; */?>" >
-->
<br>
<hr>
<?php
sleep(5);
a:
}
?>
</body>
</html>

Be careful with infinite loops. Depending on your web server (eg. apache) and php settings any request to your server adds up a thread on the server, which eats their set of ressources. This may or may not hang up your server over time.
Sometimes I use long lasting loops myself. In such cases I found apachestatus useful, to check which threads are still running:
http://httpd.apache.org/docs/2.2/mod/mod_status.html
It also provides the PID which helps to kill old threads.

Related

Erased images shown if php head program file executes

This is head file of my program:
<?php
$obrazki = array('opona1.jpg', 'olej1.jpg', 'swieca_zaplonowa1.jpg',
'drzwi1.jpg', 'kierownica1.jpg',
'termostat1.jpg', 'wycieraczka1.jpg',
'uszczelka1.jpg', 'hamulec1.jpg');
shuffle($obrazki);
?>
<html>
<head>
<title>Czesci samochodowe Janka </title>
</head>
<body>
</br>
<h1 style="color:blue;font-size: 4em; text-align:center"> Czesci samochodowe Janka</h1>
</br>
<h3 style="color:red;font-size: 2em; text-align:center"> Witamy na stronie Czesci samochodowe Janka!</h3>
<form action="glowna.php" method=post>
<div align="center">
<table width = 100%>
<tr>
<?php
for ($i = 0; $i < 4; $i++) {
echo "<td align=\"center\"><img src=\"";
echo $obrazki[$i];
echo "\"/></td>";
}
?>
</tr>
<table>
</br>
<p style="color:red;font-weight:bold; text-align:center"> Prosimy o poswiecenie czasu i poznanie nas!</p>
</br>
<tr>
<td align="center"><input type="submit" value=" Wejscie "></td>
</tr>
</div>
</form>
</body>
</html>
File got extention *.php. It has to load some pics(opona1.jpg...) from $obrazki and if button "Wejscie" is pressed, programm in file glowna.php is called. Two problems:
1. If i try to change top of the program in such way
<html>
<?php
$obrazki = array('opona1.jpg', 'olej1.jpg', 'swieca_zaplonowa1.jpg',
'drzwi1.jpg', 'kierownica1.jpg',
'termostat1.jpg', 'wycieraczka1.jpg',
'uszczelka1.jpg', 'hamulec1.jpg');
shuffle($obrazki);
?>
and to change extetion of head file from *.php to *.html - program doesn't work properly and doesn't show images at all?
2.All image files from $obrazki were of different images and had different names without 1s( like opona.jpg, olej.jpg, swieca_zaplonowa.jpg etc.)from the very begining. But if I tried to insert different images in my program and give them the same names (opona.jpg, olej.jpg, swieca_zaplonowa.jpg...) program started to show old(previous) pix even if i erased them totally. But when i changed names of all images from $obrazki adding 1s to names(like u see now) program started to show these new images.Why have I to do like that? Is this means html save information about image files somewhere? Thnx
First, always try to use English words (like variables or files) in your code. It's easier to understand for readers which don't know your language.
You can't change file extension from PHP to HTML. Only PHP files can execute PHP scripts. (Of course, you can configure your server to execute HTML file as PHP or add rule to htacess to swap extension from .php to .html)
I think it's cache. You should always refresh page few times or use any method to clean cache after making changes (In chrome you can disable cache on the selected page)

PHP Code is printing text not typed code

I have created a homepage editor tool in a script I purchased. The function of this homepage editor is to allow me to create different sections and display them one on top of the other in the order they are created. Which in hopes will give me an effect of several blocks that stretch width of the screen.
All seems to work well except one piece. I input my html and php code into the field in the admin panel and it saves to the db as I wrote it. However, when I go to echo each section back to the homepage it just displays my php code as plain text and doesn't interpret it as php and do its function.
Here is code from the homepage.php that prints the results.
<?php
session_start();
require_once("inc/config.inc.php");
if (isset($_GET['ref']) && is_numeric($_GET['ref']))
{
$ref_id = (int)$_GET['ref'];
setReferal($ref_id);
header("Location: index.php");
exit();
}
/////////////// Page config ///////////////
function get_all_section($section_id='')
{
$sql="SELECT * FROM `cashbackengine_homepage` WHERE 1";
if($section_id!="")
{
$sql.=" AND section_id='".$section_id."'";
}
$sql.=" AND section_status=1";
$sql.=" ORDER BY section_order ASC";
//echo $sql;
$res=mysql_query($sql);
while($row=mysql_fetch_array($res))
{
$section_array[]=array(
'section_id' =>$row['section_id'],
'section_name' =>$row['section_name'],
'section_desc' =>$row['section_desc'],
'section_order' =>$row['section_order'],
'section_status' =>$row['section_status'],
'last_updated' =>$row['last_updated'],
);
}
return $section_array;
}
$get_all_section=get_all_section('');
/*$get_all_section2=get_all_section('2');
$get_all_section3=get_all_section('3');
$get_all_section4=get_all_section('4');
$get_all_section5=get_all_section('5');*/
for($i=0; $i<count($get_all_section);$i++)
{
//echo htmlspecialchars_decode($get_all_section[$i]['section_desc']);
//echo htmlspecialchars_decode(stripslashes(str_replace(" ","",(str_replace("<br />","\n",$get_all_section[$i]['section_desc'])))));
echo $get_all_section[$i]['section_desc'];
}
?>
I am certain the problem has to do with the echo at the end. But I am unsure how to use htmlspecialchars to make it work with php if it even will. Or if I have to put something weird in my saved section.
Here is one of my sections. Any help is greatly appreciated. Thank you.
<div style="height:260px; width:100%; background-color:#000; margin:0px; color:white;">
<div id="header">
<div id="logo"><img src="<?php echo SITE_URL; ?>images/logo.png" alt="<?php echo SITE_TITLE; ?>" title="<?php echo SITE_TITLE; ?>" border="0" /></div>
<div class="start_saving">
<div id="links">
<?php if (MULTILINGUAL == 1 && count($languages) > 0) { ?>
<div id="languages">
<?php foreach ($languages AS $language_code => $language) { ?>
<img src="<?php echo SITE_URL; ?>images/flags/<?php echo $language_code; ?>.png" alt="<?php echo $language; ?>" border="0" />
<?php } ?>
</div>
<?php } ?>
<div id="welcome">
<?php if (isLoggedIn()) { ?>
<?php echo CBE_WELCOME; ?>, <span class="member"><?php echo $_SESSION['FirstName']; ?></span><!-- | <?php echo CBE_ACCOUNT ?>--> | <?php echo CBE_BALANCE; ?>: <span class="mbalance"><?php echo GetUserBalance($_SESSION['userid']); ?></span> | <?php echo CBE_REFERRALS; ?>: <span class="referrals"><?php echo GetReferralsTotal($_SESSION['userid']); ?></span>
<?php }else{ ?>
<a class="signup" href="<?php echo SITE_URL; ?>signup.php"><?php echo CBE_SIGNUP; ?></a> <a class="login" href="<?php echo SITE_URL; ?>login.php"><?php echo CBE_LOGIN; ?></a>
<?php } ?>
</div>
</div></div>
</div>
It looks like you're getting these section contents pieces out of your database, and not from a file stored on your web server. Is that correct?
Assuming that's true, then my next question would be, who populates this data? Is this taken in any way from user input? The reason why I ask is because of my next suggestion, which may or may not be received well.
The reason why your PHP code isn't executing, is because it's being retrieved from the database and output as a string, not as code. So how do you execute code that's stored in a string, you ask? Well, the answer to that question is to use eval() on the string. But this is where you have to be really careful!!!!!!! If any part of that string could have possibly come from an untrusted source, then malicious PHP code could be executed, which could potentially give evildoers a way into your server, where they can find all the information in your database, server, etc. Make sure you know where your code is coming from before executing it!
You make a good point that it's HTML mixed with PHP. So I see two possible solutions...
This post suggests that you could do eval(' ?>'. $section .' <?php'); This makes sense, you're breaking out of PHP before you eval your string, and so requiring the included string to open its own PHP tags to write PHP code.
Another way I can think of would be to throw the contents into a temporary file, and then include() that file:
// get contents, store in $contents
$filename = tempnam(sys_get_temp_dir(), 'section');
file_put_contents($filename, $section);
include($filename);
unlink($filename);

Php working on localhost but not on server

I've been working on some code and tested it on localhost, everything works fine, but when i upload it to my server it doesn't work beyond my php tags. No errors are showing either. I have checked both php versions and on localhost i run version 5.4.7 and on server it's version 5.3.21. Maybe this is causing the problem? Is there something I should look for in the phpinfo()? Am I missing something in my code?
Here is the code:
<!DOCTYPE html>
<?php phpinfo(); ?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<meta charset="utf-8">
<style>
body { background:black;}
.toggle { display:none; }
p {
width:570px;
text-align:justify;
color:#686868;
font-family:Georgia, serif;
}
h2 { color:black; }
button { background:#686868;
border:none;
font-family:Georgia, serif;
width:570px;
}
</style>
</head>
<body>
<?php
include('sql.php');
$i = 0;
while($i < count($rows)) {
echo "
<div>
<button class='trigger'>
<table width='100%'>
<tr>
<td width='20%'><img src='http://localhost/app-side/Icons/bar_icon.png' />
</td>
<td width='80%'><h2>{$rows[$i]['titel']} </h2></td>
</tr>
</table>
</button>
<div class='toggle'>
<p>
{$rows[$i]['info']}
</p>
</div>
</div>";
$i++;
}?>
</body>
<script>
$('.trigger').click(function() {
$(this).siblings('.toggle').slideToggle('fast');
});
</script>
</html>
When i run it, it shows a black background (as it's suppose to) but everything beyond my php starting tag gets cut off.
I have also tried to force my while loop to loop 10 times and also removed the parts where it's getting data from my database to see if it was a mysql related problem. Which I can conclude it's not.
The following line is the problem:
<img src='http://localhost/app-side/Icons/bar_icon.png
The image cannot being loaded as localhost refers to the local computer of the client (where the browser runs) not to your server. Usually such urls in websites are considered malicious ;)
A workaround to make it work on both localhost and server would be to use an relative path. This can be relative to your document or relative to the DOCUMENT_ROOT of your server. I've used the second approach as I don't know the location of your php file on server.
Solution with link relative to DOCUMENT_ROOT:
Replace:
<img src='http://localhost/app-side/Icons/bar_icon.png
by
<img src='/app-side/Icons/bar_icon.png
The only thing that I can assume is that $rows is empty and hence the following code does not get run:
// count($rows) could be equal to 0
while($i < count($rows)) { // this condition becomes false in that case
echo "
<div>
<button class='trigger'>
<table width='100%'>
<tr>
<td width='20%'><img src='http://localhost/app-side/Icons/bar_icon.png' />
</td>
<td width='80%'><h2>{$rows[$i]['titel']} </h2></td>
</tr>
</table>
</button>
<div class='toggle'>
<p>
{$rows[$i]['info']}
</p>
</div>
</div>";
$i++;
}
UPDATE
Based on your comment, it could then due to something inside sql.php, most probably database connection problem. Try putting error_reporting(E_ALL) at the very top of the page and see if that prints out any error.
Have you checked your sql.php to make sure you've changed it to match your live server (changed from localhost variables) also check your database field names versus your script for typos.
[code]
{$rows[$i]['titel']} </h2></td>
[\code]
Is it supposed to read :
[code]
{$rows[$i]['title']} </h2></td>
[\code]
If your database field name is "title" and
You are trying to get data from a field called
"titel" than that could cause your error.
I hope that helps

Loading an external PHP file with an internal PHP include function by using jQuery's 'load()'

I have a site under development with a "News" section and an "Older News" section. The news are stored individually in external PHP files which are loaded by the server by PHP (the server counts the number of PHP files and displays the last one – the current news – in the "News" sections and all the others in the "Older News" section:
<!-- ####### NEWS ####### -->
<div id="news">
<h2>NEWS</h2>
<div>
<a id="showoldnews" href="#news">OLDER</a>
</div>
<?php $directory = "assets/news/"; if (glob($directory . "*.php") != false) { $newscount = count(glob($directory . "*.php")); } else {} ?>
<?php include('assets/news/news' . $newscount . '.php'); ?>
<!-- ####### OLDER NEWS ####### -->
<div id="oldnews">
<h2>OLDER NEWS</h2>
<?php
$newscount = $newscount-1;
while ($newscount>0) {
include('assets/news/news' . $newscount .'.php');
--$newscount;
}
?>
</div>
The "Older News" section is initially hidden and only made visible by a jQuery trigger:
// Show old NEWS
$('a#showoldnews').click(function () {
$('#oldnews').show(400);
});
But I am expecting problems in long-term: since all news are loaded in the first place, this means that with more and more news coming up the website will be loading slower. And even if it's "acceptable", it's still not the ideal solution.
I was thinking on using jQuery.load() to load an external PHP file that would then load the old news only when the user asks for it.
The problem is that I don't know if its possible to load the following script by using jQuery.load() after the user clicks on a link:
<?php
$directory = "assets/news/";
if (glob($directory . "*.php") != false) {
$newscount = count(glob($directory . "*.php"));
}
else {
}
?>
<?php
include('assets/news/news' . $newscount . '.php');
?>
It this approach acceptable? I have tried, but it didn't work at all (I could load static content with the load() function, but I was not able to make the server process the PHP script. I don't know if it's possible to make the server process the PHP code, because PHP processing is done on the the server side before the website begins loading... which is not the case.
UPDATE #1
I have the following that loads the 'load.php' file from the server:
<script type="text/javascript">
$('#oldnews').load('load.php');
</script>
The content of the load.php file is:
<div class="section" id="oldnews">
<h2>OLDER NEWS</h2>
<div class="topLink">
TOP
</div>
<div class="topLink2">
<a id="hideoldnews" href="#news">HIDE</a>
</div>
<?php
$newscount = $newscount-1;
while ($newscount>0) {
include('assets/news/news' . $newscount .'.php');
--$newscount;
}
?>
</div>
The "TOP" and "HIDE" links appear without any problems. However, the PHP block seems to not be processed at all...
PHP can be run before jQuery grabs the content... Here is what I setup and tested...
test.html
<!DOCTYPE HTML>
<html>
<head>
<script type="text/JavaScript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="feeds"><b>45</b> feeds found.</div>
<script type="text/JavaScript">
$("#feeds").load("go.php");
</script>
</body>
</html>
go.php
<?php
echo 'Hi';
?>
And the result is that #feeds .innerHTML is "Hi"... I would love more information/code in replicating the situation. It might help to take a peek at the jQuery load API documentation.
As #joseph told its correct and helped me, but in mycase i wanted to hide the content of the External Php file,
Below is my code changes which had helped me !
<!DOCTYPE HTML>
<html>
<head>
<script type="text/JavaScript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<b>Db file result</b>
<div id="feeds" hidden=""></div>
<script type="text/JavaScript">
$("#feeds").load("db.php");
</script>
</body>
</html>
My Php file had database connection so i had to hide it,
db.php
<?php
session_start();
$db = pg_connect("host=ec2-xxx-x0-xxx-xx.compute-1.amazonaws.com port=5432 dbname=dxxxxxxxxx user=vxxxxxxxxxx password=1xxxxxxxxxxxxxxxxxxxxxxxxxxxxd");
pg_select($db, 'post_log', $_POST);
$query=pg_query("(SELECT id,name, FROM organization WHERE is_active = 'true' AND account_token = '".$_SESSION['account_token']."');
$json=array();
while ($student = pg_fetch_array($query)) {
$json[$student["is_user"]."-".$student["id"]] = $student["name"]."-".$student['pan'];
}
$textval = json_encode($json);
$foo = "var peoplenames=" . $textval;
file_put_contents('autocomplete-Files/'.$_SESSION['account_token'].'.js', $foo);
echo "<script>location='filename.php'</script>";
?>

Get PHP script result from a page with HTML content by AJAX

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.

Categories