LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-28-2006, 04:54 AM   #1
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Rep: Reputation: 15
Why my PHP script can't work


I write a php script.
I use header() to create a 401 HTTP auth, and log the wrong username to a file.
But, the part of log() can't work.

Why?
 
Old 11-28-2006, 05:27 AM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Can you post the exact code and the error message you get, if any?
 
Old 11-28-2006, 07:27 AM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Most likely you are logging it in the wrong way. But its imposible to say without looking at the code

Cheers!
 
Old 11-28-2006, 08:51 AM   #4
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Original Poster
Rep: Reputation: 15
Like this:
Code:
function prompt(){
         header("WWW-Authenticate: Basic realm=\".\"");
         header("HTTP/1.0 401 Unauthorized");
         echo "please input right username";
         exit;
}

for($i=0; $i<3; prompt()){
         $attempt++;
         $username = $HTTP_SERVER_VARS['AUTH_USER'];
         if(!($line = array_shift(preg_grep("/^$username$/", $authfile))))
                 return true;
         else
                continue;
}

/* My_OWN_CODE */

return false;
And, the problem is, MY_OWN_CODE never can work.

Last edited by naihe2010; 11-28-2006 at 08:52 AM.
 
Old 11-28-2006, 09:54 AM   #5
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
your MY_OWN_CODE will never be reached since you execute an exit at the end of the for loop. That means that the program will stop executing (with only one iteration).

The prompt function is used strangely. First, it should have an "exit" since it will stop the program right after sending the HTTP headers (why would you want to stop after that?). Secondly, you should call that function only once, since you cant send HTTP Headers after any output. So it should be called before the for loop, not at the end.

Another observation is that if you call "return" inside your for loop, you will also never reach the MY_OWN_CODE since it will stop the execution of the script (perhaps you meant to put a break?).

I would rethink the script because it is weird

Cheers!
 
Old 11-28-2006, 10:21 AM   #6
senyahnoj
Member
 
Registered: Jul 2004
Location: Gloucestershire, UK
Distribution: Ubuntu, Debian & Gentoo
Posts: 74

Rep: Reputation: 16
Is this sort of thing along the right lines?

PHP Code:
<?
function prompt(){
         
header("WWW-Authenticate: Basic realm=\".\"");
         
header("HTTP/1.0 401 Unauthorized");
         echo 
"please input right username";
         exit;
}

$username $HTTP_SERVER_VARS['AUTH_USER'];
if(!(
$line array_shift(preg_grep("/^$username$/"$authfile))))
        { return 
true; }

else
        {
        if(
$_COOKIE['attempt'] < 3)
                {
                
setcookie('attempt',$_COOKIE['attempt'] + 1);
                
prompt();
                }
        else
                {
                
/* My_OWN_CODE */
                
exit;
                }
        }
?>
 
Old 11-28-2006, 07:23 PM   #7
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Original Poster
Rep: Reputation: 15
Thank you, demon_vox

I delete the exit call in the prompt() function.
But, the script will execute to "return false", not loop, WHY?
 
Old 11-29-2006, 08:12 AM   #8
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi naihe,
I think that your loop is not necesary. Why do you want to do 3 times the same thing? If you want to give the user 3 tries, I think that you should take as a guide what senyahnoj has posted just above. Your web programms cannot interact directly with the user, so you cant ask the user anything in a loop. You have to send him an HTML page, wait for the user to submit, and again and again. So your loop disappears and is just execute as the user submits the form or whatever.
Perhaps you could explain a little bit more what you want to accomplish with this loop.

Cheers!
 
Old 11-29-2006, 09:52 AM   #9
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by demon_vox
Hi,


Another observation is that if you call "return" inside your for loop, you will also never reach the MY_OWN_CODE since it will stop the execution of the script (perhaps you meant to put a break?).
Might also mess up the stack and lead to mysterious bugs at a later point in the script. Best to set a flag, then break out of the loop, then test the flag to determine whether/what to return.
 
Old 11-29-2006, 05:54 PM   #10
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by demon_vox
Hi naihe,
I think that your loop is not necesary. Why do you want to do 3 times the same thing? If you want to give the user 3 tries, I think that you should take as a guide what senyahnoj has posted just above. Your web programms cannot interact directly with the user, so you cant ask the user anything in a loop. You have to send him an HTML page, wait for the user to submit, and again and again. So your loop disappears and is just execute as the user submits the form or whatever.
Perhaps you could explain a little bit more what you want to accomplish with this loop.

Cheers!
Thank you very much!
I see, now. the three times tries was send by the user's explorer, not by the server.
The loop in this script is never working.
 
Old 11-29-2006, 06:52 PM   #11
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Yes the loop is not really cool
I think you should go with senyahnoj sugestion and use a COOKIE (or SESSION or whatever) to keep track of the attempts. Plus, you'll probably learn something new

Cheers!
 
Old 11-30-2006, 11:19 AM   #12
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Rep: Reputation: 30
naihe2010 must have been a Basic developer before PHP (^_^). This is a tipical mistake of Basic coders that the exit, interrupt the code and so on.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Adding users with PHP (pass php variables to Expect script) Jayla Programming 1 10-20-2006 10:44 AM
LXer: Title: PHP/MySQL Classifieds Script AddAsset1.php Script Insertion LXer Syndicated Linux News 0 07-02-2006 06:21 PM
sudo don't work in php script maxabbr Linux - General 2 05-05-2006 11:22 AM
Trying to execute a shell-script using php but chown does not work stormrider_may Programming 15 04-11-2006 08:59 PM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 11:51 AM

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

All times are GMT -5. The time now is 03:52 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