1

I downloaded someone's project on GitHub that enabled Facebook users to send Bitcoin to each other. He abandoned development a year ago, and I want to build my own application but I am currently looking at his code and trying to get my development environment set up.

I understand PHP kinda, that its meant for server side scripting and that JavaScript is executed on a client machine (all in the browser, and Node.js is a server side framework)

Anyways, he's doing something peculiar, and its producing an error.

The directory structure is as follows:

inc/
  init.php
  app.php
  btc.php
index.php
settings.php

With many more files but the above will suffice I think.
So I run the command "php -S localhost:8000" and it starts a server

I go to localhost and once the page renders I get these errors in the terminal:

[Sat Apr 12 02:23:11 2014] PHP Notice:  Undefined variable: app_url in /home/arthur/projects/bfb/index.php on line 21

[Sat Apr 12 02:23:11 2014] PHP Notice:  Undefined variable: callback_url in /home/arthur/projects/bfb/index.php on line 22

[Sat Apr 12 02:23:11 2014] PHP Fatal error:  Call to undefined function get_x_rates() in /home/arthur/projects/bfb/index.php on line 23

[Sat Apr 12 02:23:11 2014] 127.0.0.1:59511 [200]: / - Call to undefined function get_x_rates() in /home/arthur/projects/bfb/index.php on line 23

[Sat Apr 12 02:23:11 2014] 127.0.0.1:59512 [200]: /stylesheets/reset.css

[Sat Apr 12 02:23:11 2014] 127.0.0.1:59513 [200]: /stylesheets/btc.css

[Sat Apr 12 02:23:11 2014] 127.0.0.1:59514 [200]: /javascript/chosen.css

[Sat Apr 12 02:23:21 2014] 127.0.0.1:59515 Invalid request (Unexpected EOF)

[Sat Apr 12 02:23:21 2014] 127.0.0.1:59516 Invalid request (Unexpected EOF)

The index.php file has the following general structure:

<?php
//error_reporting(E_ALL);
//ini_set("display_errors", 1);

require 'settings.php';

require 'inc/btc.php';
require 'inc/app.php';

require 'inc/init.php';

?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <meta charset="utf-8" />
        <title>Bitcoin Transactions</title>
        <link rel="stylesheet" href="stylesheets/reset.css" type="text/css" />
        <link rel="stylesheet" href="stylesheets/btc.css" type="text/css" />
        <link rel="stylesheet" href="javascript/chosen.css" type="text/css" />
        <script>var appurl='<?=$app_url?>',
                    hosturl='<?=$callback_url?>',
                    rates=<?=get_x_rates()?>;</script>
        <script type="text/javascript" src="javascript/jquery-1.7.1.min.js">    </script>
    <script type="text/javascript" src="javascript/chosen.jquery.min.js"></script>
    <script type="text/javascript" src="javascript/btc.js"></script>
</head>

<body>
 ....
</body>
</html>

Since he's loading the PHP files in the beginning of the html file, why can't it see the variables I've defined in this settings.php file:

<?
$bucket_addr='-taken-out-for-Stack-Overflow';
$callback_url='https://super-lame-name-1231232.herokuapp.com/';
$app_url='https://apps.facebook.com/c------------------n';
$blockchain_guid="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
$blockchain_pw="xxxxxxxxxxxxxxxx";
$fb_settings=array(
  'appId'  => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
);
$fb_tx_fee=10000; //in satoshi (0.0001 BTC)
$btc_tx_fee=50000; //in satoshi (0.0005 BTC)
$tx_per_page=10;
$revert_duration="60 days";

Thanks for any help!!

9
  • This may not be the cause of your issue, but it looks like the variable $fb_settings is not formed correctly as an array - there is an extra comma at the end that shouldn't be there. Commented Apr 12, 2014 at 6:58
  • 1
    @DaneIracleous Hey nice catch, it didn't solve it though. It has to do with the embedding within the <script> tag I'm pretty sure, but it really is rough debugging in a language you've never written in before haha Commented Apr 12, 2014 at 7:00
  • The problem doesn't have anything to do with where the PHP variables are being echoed within the page - those variables are undefined because the included files are somehow not actually being included. I would suggest taking the variables out of the files and then defining them directly at the top of index.php. That should solve the problem temporarily. After you've done that, you will have narrowed the problem down. Commented Apr 12, 2014 at 7:11
  • 1
    Do you think I should include the github link in case you or others wish to examine the complete source code? Commented Apr 12, 2014 at 7:16
  • 1
    I don't see a settings.php in the location that index.php expects it to be. I see a settings.example.php. Try changing that file's name to settings.php. Commented Apr 12, 2014 at 7:21

12 Answers 12

3
+25

May not be the whole picture, but I have encountered the problem with server settings before. Perhaps your server does not support the php echo shortcode of <?=. So I would suggest trying this, replace:

<script>
    var appurl='<?=$app_url?>',
        hosturl='<?=$callback_url?>',
        rates=<?=get_x_rates()?>;
</script>

with:

<script>
    var appurl='<?php echo $app_url; ?>',
        hosturl='<?php echo $callback_url; ?>',
        rates=<?php get_x_rates(); ?>;
</script>

Made a difference on my local machine. Might help. Good luck.

4
  • If you dont mind, could you explain to me why we have to echo the PHP variable, but we do not have to echo the PHP function? Thank you Chris Commented Apr 15, 2014 at 3:01
  • [Mon Apr 14 23:13:50 2014] PHP Notice: Undefined variable: app_url in /home/arthur/projects/bfb/index.php on line 21 [Mon Apr 14 23:13:50 2014] PHP Notice: Undefined variable: callback_url in /home/arthur/projects/bfb/index.php on line 22 [Mon Apr 14 23:13:50 2014] PHP Fatal error: Call to undefined function get_x_rates() in /home/arthur/projects/bfb/index.php on line 23 Commented Apr 15, 2014 at 3:14
  • literally the most aggravating thing ever. Why the hell is it undefined if its in another file? Commented Apr 15, 2014 at 3:14
  • it can be undefined for 2 reasons: either you didn't include / require the other file properly or you didn't define the function correctly... also, if an echo will be needed in front or not depends on the contents of the function... you may want to include that too in your post...
    – benomatis
    Commented Apr 19, 2014 at 15:29
2

Try to replace <? on the beginning of settings.php by <?php

1
  • Please, explain down vote. I reproduce the problem and solve it with this trick. (and I write it before the update of Salman A)
    – doydoy44
    Commented Apr 21, 2014 at 7:39
2

Maybe your php.ini is setting with no short tags active, check your php.ini settings (short_open_tag).

Or you can try to substitute the <? with <?php

3
  • If short tags setting is OFF then PHP wouldn't complain about Undefined variable in the first place. Commented Apr 21, 2014 at 6:57
  • @Salman - Actually, if the ini setting short_open_tag is disabled, the echo short tag <?= will still function (as of PHP 5.4.0) - php.net/manual/en/ini.core.php#ini.short-open-tag Commented Apr 21, 2014 at 10:19
  • The settings.php snippet uses a short tag, which would cause the variables to not be defined without short tags enabled (and then the unparsed code would appear in the page source)
    – gapple
    Commented Apr 21, 2014 at 15:29
1

My advice is to reduce mysterious guessing by using a decent PHP IDE, set breakpoints at lines shown in the error log and use the break-on-exception feature so that the IDE stops when it detects the problem. Decent IDEs also usually have a code validators built-in so the IDE may show you what is wrong even without running the code (quite likely when you are working on unstable abandoned code).

  • PHP IDE with very mature debugger and break-on-exception feature is NuSphere's PhpED - #1 PHP debugger as far as I know

  • PHP IDE with very mature code analyzers (they call it "inspections") showing many potential problems without the need to run the code is JetBrains's PhpStorm - #1 PHP code analyzers as far as I know

1

Here is what I think is happening.

The short_open_tag in the php.ini is indeed off. The file index.php uses the tags <?php and <?= in the code both of which are valid. Hence php is able to read this content.
However, the file settings.php starts with a short tag <? and hence this piece of code is imported without being treated as php code.

PHP is also able to import the files (since the requires are in index.php and is able to search for the variables. But since the variables are defined in files which cannot be read as PHP, it throws an undefined error.

Therefore, you should use a <?php tag instead of <? tag in all the files that you are importing.
There is already an answer on this post that suggests this solution sans explanation.

You will probably get more insight into the problem if you see the source code of the HTML page that is being rendered.

0

I don't think these files are being included properly. Please try to do this:

require dirname(__FILE__) . '/settings.php';
require dirname(__FILE__) . '/inc/btc.php'; 
require dirname(__FILE__) . '/inc/app.php';
require dirname(__FILE__) . '/inc/init.php';
1
  • A failed require results in a fatal error Failed opening required ...; the script would end straight away. Commented Apr 21, 2014 at 6:55
0

Try declaring the variables as global variables, by adding global keyword before them in their declaration.

0

Setting a JavaScript variables with PHP is helpful, it cannot always be done. For instance, PHP cannot modify external .js documents temporarily. It changes them for good.

You can use the below code.

<script type="text/javascript"> 
var php = <?php echo $phpVar ?>; 
</script>
0

I'm seeing that function(get_x_rates) you using on index.php is not defined, add this function into comment or check where this function is defined in any file?.

0

This was annoying me so ........................

there are a couple of issues:

  1. the files don't have closing php tags + its better to spell out

  2. Did you set up your database? test the function in btc.php by adding in $rates="hello"; return $rates; at the top of the function just under the opening function() {

i have done both and its working, just i didnt set up the database to check the function.

3
  • actually PHP recommends to omit closing php tags in php-only files php.net/manual/en/language.basic-syntax.phptags.php and it also discourages use of short <? tags recommends full <?php tags
    – xmojmr
    Commented Apr 22, 2014 at 7:43
  • and when they are loaded into another file ? what i've done there actually fixed the issue.......and i did mention using spelling out php but it got trimmed
    – David
    Commented Apr 22, 2014 at 8:48
  • I've never had a problem with php-only file loading another php-only file through require when the closing tags were missing. There is an old StackOverflow discussion here: stackoverflow.com/questions/4410704/… If there are problems with one file loading another and closing tags are the issue it is IMHO design problem. Clear separation of whats code (no closing tag) and what are templates (opening closing tags multiple times in one file). Just pointing out that your comment "..don't have closing php tags" is not a problem per se
    – xmojmr
    Commented Apr 22, 2014 at 10:18
0

Two things.

First, replace all of the <? with <?php (including in the script).

Then, you should replace (in the script) <?=$some_var?> with <?php echo $some_var; ?>.

Those are the two things I see. Hope it helps anybody who comes on this question.

1
  • I made the site with rails in the end ;) Commented Jun 24, 2015 at 19:26
0

It is very simple to do so.

Assume you have a JS variable url. So you have something like

var url = //your url

Because of the echo function of PHP, all you have to do is to run your PHP inside your script tags, and add in something like this

echo "url = //your new url"

That will cause your url variable to be edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.