PHP functions not working when not on local server - php

I'm trying to find if variables (using GET) exist using an if statement, but I can't get them to work on my web server, but my local server works fine.
I have if statements like these in my PHP:
if(isset($_GET['typeQ']) && $_GET['typeQ'] == 4 && isset($_GET['searchQ'])){
header("Location: schoolEdit.php?schoolid=".$_GET['searchQ']."");
} else if(isset($_GET['typeQ']) && $_GET['typeQ'] == 5 && isset($_GET['searchQ'])){
header("Location: vehicleEdit.php?vehicleid=".$_GET['searchQ']."");
} else if(isset($_GET['typeQ']) && $_GET['typeQ'] == 6 && isset($_GET['searchQ'])){
header("Location: driverEdit.php?driverid=".$_GET['searchQ']."");
} else if(isset($_GET['typeQ']) && $_GET['typeQ'] == 3 && isset($_GET['searchQ'])){
header("Location: studentEdit.php?studentid=".$_GET['searchQ']."");
}
When I go to this URL on my localserver (http://localhost/voyageur/index2.php?searchQ=1&typeQ=6), it sees that "typeQ" is set as 6 and "searchQ" is set to something and runs the header code. But on my web server, it doesn't run any of these using the same type of URL, it just skips them as if none of them were set.
Is there something I need to configure on my web server to get this to work properly like I have it working locally?

Try changing
header("Location: schoolEdit.php?schoolid=".$_GET['searchQ']."");
to
header("Location: schoolEdit.php?schoolid=".$_GET['searchQ']);
The header function on your server may be reading the header insert incorrectly since you're concatenated nothing to the end of your header. Hopefully, this will fix your problem. It's the only thing that I could find. Other than that, it looks sound.

Should have thought of this, but I got this to work by moving the if statements up higher in the code. Still strange how I didn't get "headers already sent" errors or something.
Thanks for the help everyone :)

Related

Why is "#" symbol still causing errors?

I have an old site using Ubuntu and PHP 5.3. There is a cron script that hits an API to push/pull data. Recently I have been hit with an error when these scripts attempt to run: "Unidentified index: UserID" in the "appsettings.php" file.
$conn=db_connect();
if(!#$_SESSION["UserID"])
{
$allowGuest=guestHasPermissions();
$scriptname=getFileNameFromURL();
if($allowGuest && $scriptname!="login.php" && $scriptname!="remind.php" && $scriptname!="register.php" && $scriptname!="registersuggest.php")
{
$_SESSION["UserID"]="Guest";
$_SESSION["GroupID"]="<Guest>";
$_SESSION["AccessLevel"]=ACCESS_LEVEL_GUEST;
$auditObj = GetAuditObject();
if($auditObj)
$auditObj->LogLogin();
if($globalEvents->exists("AfterSuccessfulLogin"))
{
$dummy=array();
$globalEvents->AfterSuccessfulLogin("","",$dummy);
}
}
}
I understand that using the "#" symbol isn't best practice, but I've been given explicit instruction to "just make it work". This has worked in the past, but since pushing up some styling changes I started getting this error.
To check to see if the USERID exists, try changing
if(!#$_SESSION["UserID"])
to
if(!isset($_SESSION["UserID"])){...
...
} else {
...// handle the error
}

ob_start() works fine in local but giving error on hosting

I am using PHP addressbook for an web application with some additional features. Everything works fine when I tested it in my local machine using Xampp but when I hosted it in a online webserver it throws the following error message:
[ref.outcontrol]: output handler
'ob_gzhandler' cannot be used twice in .../dbconnect.php on line 27
... and the page is not showing anything its just an empty page.
if( ini_get('zlib.output_compression') != 1
&& isset($compression_level)
&& $compression_level > 0) {
ini_set('zlib.output_compression_level', $compression_level);
ob_start('ob_gzhandler');
}
Please help me to solve this issue. My gzip compressor is in off.
To overcome this just put a if check like this before adding it
if (!in_array('ob_gzhandler', ob_list_handlers())) {
ob_start('ob_gzhandler');
} else {
ob_start();
}
It says:
output handler 'ob_gzhandler' cannot be used twice in
.../dbconnect.php
You can not call ob_start('ob_gzhandler') more than once in your code.

php header() working in localhost but not working on live server?

if ($num_rows == 0 && $num_rows2 == 0) {
echo 'result1';
header('Location:enter-work.php');
die();
} else if ($num_rows >= 0 || $num_rows2 >= 0) {
echo 'result2';
header('Location:similar-work.php');
die();
}
I have the above code that I'm testing with and it properly echos out the sample text 'result1' or 'result2' every single time I refresh the page with this code, but it is not redirecting to enter-work.php or similar-work.php like it does on my localhost server. On my localhost server, I can even link it to online websites (ie. google.com). And I inputed the above code before any html was written - so before even the doctype was declared.
Not sure what's causing the problem, need some help.
EDIT: as Pekka mentioned in his comments (on the selected answer) the problem was I had echos and white spaces before the header() function.
You mustn't output any content before sending headers.
That the local and live server behave differently if you do, may be because of different output buffering settings.
Anyway, it should work if you remove the echos.
for any one still face same problem :
first try just simple header() code in yourfile.php and remove any other content if the problem solved that is mean the problem in your old code.
second : header () should work even with another content like echo "" etc ...
last :if you tried simple header() code and the the problem still not solved contact your hosting provider.

include() it does not work

I have problem that on server include() function does not want to work and I have no idea why?
I have:
if (file_exists('/home/p002/htdocs/Project2/library/IntelliSMS/SendScripts/IntelliSMS.php')) {
echo "1 works";
} else {
echo "The file 1 does not exist";
}
if(include "$_SERVER[DOCUMENT_ROOT]/Project2/library/IntelliSMS/SendScripts/IntelliSMS.php" == 'OK')
{
echo 'INCLUDE 1 works';
}
else
{
echo 'Step 1 fail';
}
if(include '/home/p002/htdocs/Project2/library/IntelliSMS/SendScripts/IntelliSMS.php' == 'OK')
{
echo 'INCLUDE 2 works';
}
else
{
echo 'Step 2 fail';
}
It returns: 1 works Step 1 fail Step 2 fail
I have no idea how to force it to work. HELP
I use zend framework and this file is in the library (parallel to zend -libraries - directory but it does not want to work too without the include directory :/
It is really strange for me as when I add:
include ("/home/p002/htdocs/Project2/library/IntelliSMS/SendScripts/IntelliSMS.php");
And it is correct path I have only blank page! But when I add:
include ("/homedddd/p002/htdocs/Project2/library/IntelliSMS/SendScripts/IntelliSMS.php");
And it is wrong path the page is not blank, it looks like working OK. It mean something is wrong with this IntelliSMS library it does not work with my server but I do not know why? Probably server blocking sending sms or something? Do you have any idea? This library is from http://intellisms.co.uk/sms-gateway/php-sdk/ Maybe there is problem that it needs the OpenSSL extension module? What should I do it to start works?
In the first block of code you found out, that /home/p002/htdocs/Project2/library/IntelliSMS/SendScripts/IntelliSMS.php exists. When you try to include this file, you never use the exact same string. If you do, it should work.
Edit:
You tried that and it failed. In that case it looks like a permission issue. If you do not have read permissions on that file, the first test will work, but including the file will fail.
You've changed your slashes around. This may be a problem.
You should never use the \ backslash in path names. Use the forward slash / which works on both Windows and Un*x servers.
Also the base directory name is unlikely to be identical on both servers. Make it relocatable and replace /home/p003/htdocs with DOCUMENT_ROOT like so:
include "$_SERVER[DOCUMENT_ROOT]/Project2/library/IntelliSMS/SendScripts/IntelliSMS.php";
Notice the double quotes.
I would further recommend to avoid mixed-case filenames if possible.
Here's what you're doing wrong, you were actually trying to include FALSE in every single attempt. Doing something like this
include '/path/to/file.php' == "OK";
equals that
include FALSE;
This is probably what you wanted to do
if((include '/path/to/file.php') == "OK") { echo("works"); }
Other SO users overlooked this fact because your path is so long, the comparison gets hidden in your code block.

PHP voting code works on 5.2.5 but not on 5.2.11 anymore

Ok, so a little while back I had some help writing some PHP voting code, it worked just fine after I upgraded my server to use the latest version of PHP. However now I have switched servers, and the PHP isn't as up to date as the other one. Anyways here's my code:
<?php
if(!file_exists('vote/1u.txt')){
file_put_contents('vote/1u.txt', '+1');
}
if($_GET['click'] == 'up1'){
file_put_contents('vote/1u.txt', ((int) file_get_contents('vote/1u.txt')) + 1);
header('Location: ' . $_SERVER['SCRIPT_NAME']);
die;
}
?>
Execute and display:
<img src="images/thumbsup.jpg" width="40px"border="0"> <br>Votes: <?php echo file_get_contents('vote/up1.txt'); ?>
Now when on my other server (PHP version 5.2.5) this code worked great! However on my new server the PHP version is 5.2.11, and because of this the code won't work. My question is, is there any way to make this more compatible with an earlier version of PHP, or to write completely new code that will work just like this one? Or is there a way to tell my servers to use PHP 5.2.5+? I'm using cPanel X admin panel.
I have set the text file permissions to 777 and still nothing!
you are checking for variable "click" but executing the code only if it equals "up1".
But your link tells click to equals "yes" so that part of the code is never true, hence never executed.
Change your executor to this:
<img src="images/thumbsup.jpg" width="40px"border="0"> <br>Votes: <?php echo file_get_contents('counteru.txt'); ?>
But more logically, your processing code should be rationalized a bit to this:
if the link is clicked :
First, if the data file (lu.txt) does not exist, create it and write '+1' inside of it, else, add 1 to its existing value.
Then, redirects to the initial page.
if($_GET['click'] == 'up1'){
if(!file_exists('vote/1u.txt')){
file_put_contents('vote/1u.txt', '+1');
}else{
$content = file_get_contents('vote/1u.txt');
if(!$content){
die("Error! file_get_content failed !");
}
file_put_contents('vote/1u.txt', ((int)$content) + 1);
}
header('Location: ' . $_SERVER['SCRIPT_NAME']);
}
exit;
Not a bad idea to add a trim() around file_get_contents(). Or to check if $_GET['click'] isset() prior to checking if it's equal to 'up1'.
It's conventional to exit() instead of die() after a header redirect--well, from what I've seen at least.
Basically, during development, turn on error reporting and set your error flag to E_ALL to see everything, including warnings and notices--neither of which halt your code, but should still be known and addressed.
You might discover the reason your code produces different outcomes under different minor versions of PHP by turning on full error reporting.

Categories