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.
Related
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
file_get_contents with query string
I'm using the file_get_contents function but although returning the correct output, it is still showing this error:
Warning: file_get_contents(secure/validate.php?cardnumber=1234567) [function.file-get-contents]: failed to open stream: No error in ...
The scenario is card number validation and in validatecard.php there is a simple if statement:
if (isset($_GET['cardnumber']) && ($_GET['cardnumber'] == "12345")) {
echo "OK";
} else {
echo "INVALID CARD";
}
My code is:
$cardnumber = $_POST["cardnumber"];
$url = "secure/validate.php?cardnumber=" . $cardnumber;
if (file_get_contents($url) != "OK"){
$order_error_msg = "Invalid card number";
} else { ....
What may be the problem?
Well, it seems like you don't have allow_url_fopen set in your php.ini #Gordon is correct, this is not a url_fopen issue. It's actually failing because using file_get_contents on the local file will actually get you the code for the file, not the PHP-processed result of running that file. To get it to work as you wanted, you'd need to hit apache/PHP by prepending "https://localhost/" to the url, and enabling allow_url_fopen.
But also this looks like a very worrying piece of code; you should do as little as possible with CC numbers in the code. By using file_get_contents and a card number on the get string, it opens up the possibility of the number being logged somewhere.
A much more secure implementation would look something like this:
validatecard.php
function checkCard($card) {
if ($card == "12345")) {
return "OK";
} else {
return "INVALID CARD";
}
}
Then in your main code:
include('secure/validatecard.php');
$cardnumber = $_POST["cardnumber"];
if (checkCard($cardnumber) != "OK"){
$order_error_msg = "Invalid card number";
} else { ....
That way your checkCard function is more re-usable, and you don't have to ferry the card number around so much.
If you decide to go with the file_get_contents approach and hit https://localhost/secure/validatecard.php?card=12345 then the credit card numbers will get logged in your apache access logs in plain text. This is verging on criminally negligent, don't do it.
also, as per Gordon's advice, make sure that you're using https all the way through.
You might consider hiring in a contractor with experience writing shopping carts/checkouts. These things are important to get right, and can be insecure in subtle ways if you're not experienced.
are you sure your php.ini configuration allows for opening urls?
you can check using phpinfo() and searching for allow_url_fopen
also, as another poster noted , using GET for this kind of stuff isn't really ideal (read: really really bad). if you're keen on making a request to another page, rather than using a file (if that other page is not on your server, for example), try using cURL and do a POST request
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.
I have two servers. I want delete file from second server via first server!
For example:
first-server.com
second-server.com
I have made two php files - file on first server and file on second server.
The file on first server contains
files.php
while($file = mysql_fetch_array($files){
echo $file['file_name'];
echo 'Delete';
}
the file on second server contains
delete.php
if($_GET['file']){
if(file_exists($_GET['file']){
unlink($_GET['file'];
//file deleted !
}
No it's ok , but. I want done this job without redirect me or visitor to the second server
For example : ajax or curl or something like that. What is the best way to do that?
Edit.
The codes above is just tests. It's not my real files. Please help in the way to process delete request without redirect to second server php file.
I think a simple file_get_contents is enough:
File on first server:
$result = file_get_contents('second-sercer.com/delete.php?file=text.txt&some_security_token=asd');
//From $result you will know what was the result on the other server
File on second server (delete.php);
if($_GET['some_security_token'] == "asd"){
if(file_exists($_GET['file']){
if(unlink($_GET['file'])){
//File deleted we are cool
echo 1;
} else {
//File deletion failed
echo 0;
}
}else{
//File don't exists
echo -1;
}
}else{
//bad token
echo -2;
}
So this way your first server on script level goes to the second server so you can check parameters before that. And the second server sends back error / success codes so you can handle them on first server:
1 - success
0 - failed deletion
-1 - file doesn't even exists
-2 - bad security token
I do not include a way to create a token that both of the servers know. You can hash the file name with some key value for start, but you have to make it expensive to guess. I just try to point out that you need this kind of security too to make it even safer. And you have to find out a way to protect file system from deleting files that important for second-server. For example you only let the deletion of files in some folder only.
You could use cURl too the same way for this. But always try to return info for the first-server.com about the process on the second-server.com
unset unsets a variable, it doesn't have anything to do with files.
You're looking for unlink.
BTW, you should do some serious validation on what you're going to unlink. Just blindly accepting anything in the URL can have serious consequences.
http://second_server.com/delete.php?file=delete.php
Delete file
<?php if ($foo = $_GET['file']) {
echo "<img src=\"http://second_server.com/delete.php?file=$foo\" style=\"display:none;\"><script>alert('deleted');</script>"; }
?>
First of all, you want a security code or token or the like, so that unauthorised people do not delete files from your server.
while($file = mysql_fetch_array($files){
echo $file['file_name'];
echo 'Delete';
}
and in first_server.com/delete.php, put this:
file_get_contents('second-server.com/delete.php?file=' . $_GET['file'] . '&securitycode=thisisasecuritycode');
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.
file1.php and file2.php with die(); function.
include.php:
<? include 'file1.php';
include 'file2.php' ?>
file1.php
<? echo 'included'; die(); ?>
file2.php
<? echo 'not included'; die(); ?>
How can I include both files with die(); function?
Non-English Speakers:
You can provide your question in your native language as well, and somebody here may be able to translate it for you. Just make your best effort to ask in English, and add your native tongue below.
If you would like to test whether the includes happened successfully, you can test the return value of the include function itself:
// http://us3.php.net/manual/en/function.include.php Example #4
if ((include 'file1.php') != 'OK') {
die();
}
You may also consider require() instead of include() depending on your needs:
require() is identical to include() except upon failure it will also produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.
If I understand correctly what you are trying to do then unfortunately it isn't possible.
die(); will stop the script from executing at the point from where it is called.
Here is how to include a file, or die with a message if the include fails code sample.
(include('file.php')) || die('Failed to include file!');
if (!condition){
include_once('./inc/header.inc.php');
echo "Errormessage";
include_once('./inc/footer.inc.php');
die();
}
I hope this is what you wanted.
Just a minor improvement to LorenzoP's method:
(#include("file.php")) or die("Failed to include!");
// notice the # sign!
This way, you save yourself 2 ugly lines of php warning when inclusion fails. Otherwise I think this is truly the best way to handle failed includes. (Also, his answer should be the accepted one.)
If the execution of your included file is not dependent on the current file (no shared variables, functions, etc.), then use
file_get_contents('link_to_file.php');
instead of an include method. When you force the file to execute independently it will not make a effect in the script execution.
die() is just an exit with an error, you can't include files with it and I don't really understand why you want to. Could you provide more details as to what you're trying to accomplish?