LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2005, 10:14 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
Reoccuring error when trying to code php


I have the knoppix live cd version of linux. When trying to code the following simple program:

<?
$vars = explode(", ", urldecode($_SERVER["QUERY_STRING"]));
foreach ($vars as $var){
print $var."<br>";
}
?>

I keep getting the same errors:

./PHPServer2.php: line 1: ? No such file or directory
./PHPServer2.php: line 2: Syntax error near unexpected token (
./PHPServer2.php: line 2: '$vars = explode(", ", urldecode($_SERVER["QUERY_STRING"]));

Can anybody tell me what the problem is. I wrote the code in the vi text editor and compiled it by using:

chmod 711 PHPServer2.php
 
Old 06-02-2005, 01:07 PM   #2
meonkeys
Member
 
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
Try the following:

1. instead of starting your block of PHP code with '<?', use '<?php'. This is configured with the 'short_open_tag' in your PHP configuration (probably /etc/php.ini).
2. if you're running from the command line, try 'php PHPServer2.php' (or whatever you named your script) instead of './PHPServer2.php'.

The errors are from the shell. If you're trying to run this script from the command line, $_SERVER["QUERY_STRING"] is probably not going to be defined. Also, if you wish to make the script executable and run it directly (./PHPServer2.php instead of 'php PHPServer2.php') from the command line, you need a shebang line. Try adding '#!/usr/bin/php' to the beginning of the file.

Shell mechanics can be a little tricky at first. The Advanced Bash-Scripting Guide is an excellent resource for shell programming.
 
Old 06-02-2005, 01:19 PM   #3
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Thanks alot of your help. I still got an error on the line with #!/usr/bin/php

bad interpreter: no such file or directory

Do I have configure php or apache for knoppix live cd? Could that be the problem and if so how woulf you go about doing that. When I go to the desktop and go into the moutable harddrive I can see the file created as a php file yet it still throws the error above. Any other suggestions?. I dowloaded this live cd becasue everyone told me how good knoppix was.....I haven't even been able to run a 5-6 line program yet.
 
Old 06-02-2005, 01:20 PM   #4
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Also if I can't run it from the command line....whereelse and how else do I run it?
 
Old 06-02-2005, 01:46 PM   #5
meonkeys
Member
 
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
Quote:
I still got an error on the line with #!/usr/bin/php

bad interpreter: no such file or directory
Hmm, maybe you don't have php, or maybe it's installed somewhere else besides /usr/bin.

Try 'which php' to see if it's in your path, then change the path in the shebang line to the result of 'which php'.

If this doesn't work, check out the PHP documentation for details on how to install PHP and execute PHP scripts.
 
Old 06-02-2005, 01:57 PM   #6
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
On my desktop, i can go to the etc file and in there is a php4 file and apache. This would mean that php is installed right?.......Inside the php4 file is apache and then inside that is php.ini....what do you think?
 
Old 06-02-2005, 02:08 PM   #7
meonkeys
Member
 
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
Try 'which php' to see if it's in your path, then change the path in the shebang line to the result of 'which php'.
 
Old 06-03-2005, 07:16 AM   #8
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Do you mean to type which php on the command line?

I typed which php on the command line and nothing happened......it just brought up a new command line.

I alos tried whichphp as one word and got a command not found error.

Any other ideas on how I might get this working?
 
Old 06-03-2005, 10:21 AM   #9
meonkeys
Member
 
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
Quote:
I typed which php on the command line and nothing happened......it just brought up a new command line.
Hmm, ok, since you don't have the CLI SAPI installed, try this (on the command line):

Code:
$ sudo /etc/init.d/apache start
Note that the '$' represents the prompt, which may be a '#' or something else. Don't type in the '$'. Now the Apache Web server should be running. Then, write/copy the .php script you created into /var/www/test.php. Here's one way to do this:

Code:
$ sudo cat > /var/www/test.php
<?php phpinfo(); ?>
Hit CONTROL-d to stop typing. Ok, now you have a php script called test.php in the default document root for Knoppix (I'm using version 3.8.2).

You should be able to view it in a Web browser by browsing to 'http://localhost/test.php', or, on the command line:

Hope this helps.
 
Old 06-03-2005, 10:39 AM   #10
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
Where do I type the name of my php script? I see you say to type:

$ sudo cat > /var/www/test.php
<?php phpinfo(); ?>

But where do I put the name of my script. I called it PHPServer1.php.
 
Old 06-03-2005, 11:59 AM   #11
meonkeys
Member
 
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
Copy your script into /var/www/. Name it whatever you choose.
 
Old 06-03-2005, 12:11 PM   #12
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
I used the following command:

$ sudo cat > /var/www/PHPServer1.php
<?php phpinfo(); ?>

$ lynx http://127.0.0.1/PHPServer1.php
and got the following errors:

Warning: Unknown (/UNIOFS/var/www/PHPServer1.php):failed to open stream. Permission denied in unknown line 0

Warning: (null)(): Failed opening '/UNIOFS/var/www/PHPServer1.php' for inclusion (include_path = '.:/usr/share/php : /usr/share/peak) in unknown on line 0

I do not have internet access on the computer that I am running linux on. Do I need to get that? Is it possible to access it from a computer with internet access. Also here is the code that I am using in the mean time:

<?
while (1);
//Attempt to read string from socket
//For now let's pretend it's been read successfully
//and is called "stuff"
$stuff = "1,2.2,3.245,.546";

if ($stuff != "") {
$array_of_stuff = explode(',', $stuff); //Now it's an array
//Display them:
print ($array_of_stuff[0]);
print ($array_of_stuff[1]);
print ($array_of_stuff[2]);
print ($array_of_stuff[3]);
$stuff = "";
}
?>


As you can see I just defined a fixed string. What do I need to change in my code so that it accepts the dynamically changing string passed from my client as opposed to this "dummy" string that I have made up?
 
Old 06-03-2005, 12:33 PM   #13
meonkeys
Member
 
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
For the permission denied error, the file needs to be readable. See the manpage for chmod (man chmod).

Your code looks fine, but it will probably have to start with a '<?php' preamble instead of just '<?', but I may be wrong.

As for your code, you may either (a) pay me to write it for you or (b) see the plentiful online PHP documentation.
 
Old 06-03-2005, 01:01 PM   #14
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
What do I have to do with the manpage for chmod (man chmod). How do I make the file readable.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With PHP Code windisch Programming 4 09-09-2005 07:37 AM
PHP code question latino Programming 2 08-26-2004 04:57 AM
reoccuring /dev/null error on bootup mehesque Linux - Newbie 3 01-13-2004 04:48 PM
php code lynger Linux - Software 0 01-07-2004 02:21 AM
hard drive problems, reoccuring problem pippy Linux - General 0 12-09-2001 10:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration