LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-12-2017, 03:59 PM   #1
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 119

Rep: Reputation: 15
PHP if-statement ignores $_POST value


I want to put an if-statement at the top of a PHP web-page, to confirm that the user has arrived there after using a valid password. The code I've tried is this:
Code:
<?php
  if ($_POST["psswd"] == "password") {
    search_db();
    exit;
  } else {
    header("Location: http://www.junk.com");
    exit;
  }
?>
Now, I know, beyond question, that the 'psswd' value is passed from the previous page, because when I replace the code with this:
Code:
<?php
  echo $_POST["psswd"] . "<br /><br />";
?>
the correct password shows up in the browser window.

In both cases, this code is the very first thing in the document. There are no blank spaces nor blank lines above or before it. However, the first bit of code consistently fails in that it invariably goes to the 'else' portion of the if-statement and displays the junk.com web-page. It does not run the search_db() function (which I've tested and found reliable).

I've also tried doing this by using a not-equal comparison in the if-statement (using both <> and !=), calling the header in that case, and not using an else-statement to call the search_db() function in that section of code at all, but calling it farther down in the script.

How can I revise my code to get the if-statement to work?
 
Old 04-12-2017, 04:55 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,642

Rep: Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190Reputation: 4190
I don't see anything obviously wrong with your if statement. I assume the form is ok since you wrote that you can see the value by the echo command but can you post it anyway. You can check if there are any errors by looking at the apache logs at /var/log/apache2/error.log. I'm guessing at the log file path... I am also assuming that you are using the correct case for your password.
 
Old 04-12-2017, 07:57 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,240
Blog Entries: 4

Rep: Reputation: 3265Reputation: 3265Reputation: 3265Reputation: 3265Reputation: 3265Reputation: 3265Reputation: 3265Reputation: 3265Reputation: 3265Reputation: 3265Reputation: 3265
Remember that $_POST will only include values that the browser actually posted. If the field is empty, it might not be included in the post-stream. In this case, it won't be in the superglobal, either.

By far the easiest way to sort this out is to use the debugging features of the browser, to let you s-e-e what the browser did and did not include. You can also dump the content of the array, server-side.
 
Old 04-12-2017, 08:54 PM   #4
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 119

Original Poster
Rep: Reputation: 15
sundialsvcs, I do know that about $_POST, which is why I tested the page with
Code:
echo $_POST["psswd"]
which returned the datum I was looking for.
 
  


Reply

Tags
header, php


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
php question regarding $_POST and $_GET robertjinx Linux - Software 5 01-09-2016 01:51 PM
changing contents of $_POST in PHP jiml8 Programming 2 04-04-2006 09:11 AM
Python ignores global statement? voyciz Programming 3 04-08-2005 02:07 PM
Large file upload - $_POST empty in PHP PsychosisNode Linux - Software 1 01-23-2005 07:32 AM
PHP : $_POST['choice$i'] doesnt work WindowsBurner Programming 7 12-18-2004 08:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:43 PM.

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