PHP files not loading properly - php

I have a PHP file consisting of the following structure:
<html>... headers, scripts & styling
... some html here
<?php
if($_GET['v'] == 1)
{
?>
... html code here ...
<?php
}
else
{
?>
... html code here ...
<?php
}
?>
</html>
Sometimes the file just loads half, for example if v=1 what would load onto the screen (if I check with View Source also) is something like this: (relative to what I exampled above)
<html>... headers, scripts & styling
... some html here
... html cod
As you can see, the code just cuts off randomly. The is nothing obvious casing this such as a loop or anything. It happens in the middle of HTML code and not inside the <?php ?> tags.
It looks as if the server just decides to stop communicating right there-and-then for no reason. It also happens at a different and random place each time and sometimes loads perfectly fine.
It also only happens on my shared hosting account and not on my localhost.
Is there anything simples that might be causing this?
Did anyone experience this before?

Your code produces a warning (apparently silent) and fails here:
if($_GET['v'] == 1)
if no v parameter was given in the query string.
Do it like this:
if(isset($_GET['v']) && $_GET['v'] == 1)
If you're running an old version of PHP you'll have to make two separate if statements for each of the two conditions.

Make sure you have display_errors turned on.
ini_set('display_errors',1);
Just to make sure there's nothing going horribly wrong.

Related

Running PHP code from a database

Ive got some code that runs html code from a database in order to not have to make a new file for every page that I might want to make, problem with this is that if the page contains php code it wont run, and im pretty sure you can do this with eval however it has security risks so I was trying to find some alternatives. I can paste the code if necessary. Ive got a php script that gets data from table and a main php script that gets the formatting in HTML and PHP for the data however it will just run the PHP code as if it were strings from the database.
Here is an image of the code thats meant to be run from the php script :
Here is the php code in that row
And this is the main script that is meant to run it :
Why not make PHP functions? Like, for example if you wanted to spit out data from your databases using PHP, but not a lot of code, you can do something like... (for user profiles)
Like, you can make a funcs.php
then inside of it do:
function user_page($user_id){
?> [Create user page here] <?php
}
Then inside of any other file just do:
include 'funcs.php';
if(isset($_GET['username']) === true){
user_page($_GET['username']);
} else {
//if not loading user page then do something else
}
EDIT
Okay, I just saw your screenshot.
To do something like that, a function would be good.
Ex:
function print_data($info1,$info2,$info3,$info4){
echo("<center>$info1</center><br>$info2<br>$info3<br>$info4");
}
then just calling the function with the $row[] information you have in your screenshot.
Like so:
print_data($row['email'],$row['name'],$row['username'],$row['ip_addr']);
this question must be added to some "doing it wrong" list, honestly.
but, returning to your question you have 3 options, all of them more or less painful, and only one of them is right.
to do it right: rewrite your engine or take some cms/framework. store text data in database, scripts/template on disk
eval! (which you don't want)
parse. (it will be very hard and slow and totally crazy)

$_GET is not working

Creating a rating system and the info is not being transmitted through my $_GET variable. The code is below
if (isset($_GET['item'], $_GET['rating'])){
echo 'Works!';
}
The variable is being entered in this code below
<?php echo number_format(
$article['rating'],1); ?>
<div class = "rate">
Rate:
<?php
for ($x =1; $x<= $maximum_rating; $x++){
?>
<a href="prestige.php?item=<?php echo $article['id']; ?>&rating=<?php echo $x;?>">
<?php echo $x; ?></a>
<?php
}
?>
I am fairly new to programming so any ideas or tips would be greatly appreciated.
There are a couple of things you should do.
1.
Instead of
prestige.php?item=<?php echo $article['id']; ?>&rating=<?php echo $x;?>
Use
prestige.php?<?= http_build_query(array('item' => $article['id'], 'rating' => $x), '&') ?>
This will escape the parameters. Vars $article['id'] and $x could contain characters that break the HTML or URL.
2.
Look at the Net tab in your Firebug/Chrome dev toolbar. Are there any redirects? What headers are sent?
Also look at the address bar to see if prestige.php really is loaded with the GET parameters.
3.
Use a debug tool like XDebug to step through your code. You might have some code that resets the $_GET vars. Personally I use the IDE PHPed, but it's kinda expensive.
The code you posted works. So the snag must be in the code you did not post:
maybe the prestige.php page has a PHP error that prevents it from displaying anything; start with an empty file containing just <?php echo 'OK so far'; ?>.
maybe the page contains code (security checks, frameworks...) that kills $_GET. (reduce the page to a minimum working case, without include/requires)
maybe the page does work, but the output gets snarked by an untimely ob_end_clean() that was meant to "clean the page" before the real output started; (reduce the page to a minimum working case)
maybe the page works, the string 'Works' is there, but you can't see it due to HTML markup, CSS, or other rendering problems (check the page source)
the URL might be broken because the item code contains invalid URL characters (check what appears in the browser address bar)
there might be an URL rewrite scheme that interferes (check .htaccess and the server logs)
I just remembered something like this happening with international characters in the URL. Try with an ASCII-clean item code to see what happens.
Just to be sure: verify there is no auto_prepend'ed file which might interfere.
Then, it might also be more than one of the above acting together. Often when debugging one unintentionally breaks some code, and even after fixing the first bug, the code doesn't start working again - this doesn't mean the fix was invalid.
I'm sorry -- I'm at the end of my options. I really look forward to knowing what the reason was. (Usually the more explanations I amass, the more the real answer tends to be "none of the above". When it happens to me, sometimes I wonder whether to start to believe in gremlins :-( ).

javascript inside php code

i have place javascript inside php code, like this
<?php if(condition) { ?><script>do some scripting here</script><?php } ?>
it works perfect when i run it in local.
but when i upload it to the server it doesnt run..
the javascript doesnt work.
Can anyone please help.
thanks,
Devan
First, I'm pretty sure you have to do either:
<?php if (condition) { ?> ** script stuff ** <?php } ?>
or
<?php if (condition) : ?> ** script stuff ** <?php endif; ?>
(Your script has no scope on the if statement, although that could be a copy/paste error).
Second, it could be your script (perhaps one of the Javascript files such as jQuery isn't on your server).
Your problem is pretty vague...
Here's all the things I would review first :
are you sure php runs on your distant server ?
do you have anything on screen when running your script ?
when you view the source code from the generated page : do you see your javascript code ?
did you check for javascript error messages ?
Another batch of questions after the comments :)
Did you try to change your JS code with a "basic" one, such as "alert('my code works');" ? (in order to see if your javascript code is triggered)
If the basic alert code does not produce a popup, then you should see your browser's setup with javascript. It may not be enabled
if it works (and you don't have any JS error messages) : try to place some more "alert" popups along your own code, in order to find where your code stops to work)
This will not provide you a direct answer to your problem, but will help you to find more details on your problem, and will help us find an answer

Tag <? Misbehaving in PHP

I'm working with a legacy code that someone had left, and it happens to be my task to re-deploy the code. I'm using Windows Server 2003, Apache 2.0.63 and PHP 5.2.10.
It doesn't work. At least, not in the way I had expected it to work. Call it bugs, if you will.
Upon inspecting, I had a suspicion that this code (which appears numerous times in the application) is the culprit.
&$this->
To illustrate the problem, I reproduce this code:
<?php
phpinfo();
//$variable = &$this->request;
?>
The code above executed beautifully and as expected. However, if I change the code to:
<?
phpinfo();
//$variable = &$this->request;
?>
The code misbehaves, and produce this result on the screen instead, which of course, totally unwanted and unexpected.
request; ?>
Now, the code is littered with the similar code as above, and as such, the application now produce output on the screen similar to this one:
request; $user = &$this->user; // This is comment return false; ?>
for a code that reads as:
<?
$request = &$this->request;
$user = &$this->user;
// This is comment
return false;
?>
I had tried to change every <? with <?php whenever &$this-> rears its ugly head, but most of the time, it introduces a new error instead.
I reinstalled PHP and Apache, even using another version of PHP (5.2.6) and it still won't work. I deployed the code in my localhost (Mac OS X, PHP 5.2.8 and Apache 2.0.63) and it worked without a hassle.
Please, anyone, any enlightenment will more than suffice.
In your php.ini, you need to set the following directive:
short_open_tag = On
From the manual:
Tells whether the short form (<? ?>)
of PHP's open tag should be allowed...
If you have time on your hands, you may want to consider replacing all those short tags '<?' with the full-form ones <?php, for better portability (see what just happened to you? :)
my recommendation is not to use open tags, because it can interfere with <?xml codes.
I also had that problem before, just go and replace all <?php to <? , and then again all <? to <?php.
In this way you won't get any
<? <-- one space after the '?'
and
<?php <-- one space after the 'p'
hope this will help...
If you want to use the short tags:
("<?")
they need to be enabled in you php.ini. See this link.

PHP system function strange behavior

I have a strange behavior with PHP system function. In this php script there are only 2 instructions (the rest being pure html):
<?php echo system('cgi-bin/gallery2/galleryheaderview.cgi'); ?>
<?php echo system('cgi-bin/gallery2/galleryview.cgi'); ?>
The first cgi just returns a single line as you can check here
http://reboltutorial.com/cgi-bin/gallery2/galleryheaderview.cgi
It returns
My Gallery
But the whole php script returns My Gallery Twice:
My Gallery
My Gallery
http://reboltutorial.com/gallery2.php
Is there a reason (I don't use My Gallery in second cgi script of course see http://reboltutorial.com/cgi-bin/gallery2/galleryview.cgi) and how to prevent this ?
Thanks.
Update: The system function will do two things. The first is, it will run a command and pass its output through to the browser and/or output buffer. The second is, it will return the last line of output. So when you're saying
echo system('/...');
You're saying "Hey system, output the results of this command" and then "Hey ehco, output whatever system returns". Removing the echo
system('/...');
will fix your problem.
A few other things to check
Are you sure its galleryheaderview.cgi that's returning things twice? Comment out the include to make sure its actually the script that's echoing My Gallery twice
Is your PHP page/program included/constructed in such a way that galleryheaderview.cgi is being called twice?
Are you sure that calling the URL http://reboltutorial.com/cgi-bin/gallery2/galleryheaderview.cgi is calling the same command line as cgi-bin/gallery2/galleryheaderview.cgi?
If you've checked out the three items above, you'll need to drop into the source of galleryheaderview.cgi and see why its outputting the header twice.
Are you absolutely sure that nothing else is outputting the My Gallery before this line? You should try removing it, and see if it goes completely away or if there is still is one "My Gallery"
<?php echo system('cgi-bin/gallery2/galleryheaderview.cgi'); ?>
If this doesn't bring you any further, maybe you have included some php file twice?

Categories