-1

I have a PHP project in which I have an index.html with a simple form:

    <p>Test connection</p>
    <form action="Servicios.php" method="post">
        <input type='submit' name='Test' value='Test'>
    </form>

in Servicios.php Im trying to process it like this

<?php    
   echo "can you print this";
    if($_SERVER["REQUEST_METHOD"]=="POST" )
    {
      if(!empty($_POST["Test"]))
      {
         echo "Hello world";
      }
    }

But it doesn't works, thats because it never evaluates the first if like "true". The first echo at the top does works but if I do an echo to $_SERVER["REQUEST_METHOD"] it doesn't give me anything. I've tried with isset($_POST['Hola']) but I had the same result.

This only happens in the project I have in an internet host. I wrote this exact same code in my local computer using netbeans and xampp and it works perfectly. I have no idea why.

I have the feeling that I'm making some silly mistake but I cant find it.

My host is an Ubuntu server from the ec2 Amazon Web Services.

Edit this is the output of <?=print_r($_SERVER);?> in Servicios.php I replaced the parts where my ip is showed with [ip]

Array ( 
    [HTTP_HOST] => [ip] 
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0 
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
    [HTTP_ACCEPT_LANGUAGE] => es-MX,es-ES;q=0.9,es;q=0.7,es-AR;q=0.6,es-CL;q=0.4,en-US;q=0.3,en;q=0.1 
    [HTTP_ACCEPT_ENCODING] => gzip, deflate 
    [HTTP_REFERER] => http://[ip]/ProyectoPM/ 
    [CONTENT_TYPE] => application/x-www-form-urlencoded 
    [CONTENT_LENGTH] => 11 
    [HTTP_CONNECTION] => keep-alive 
    [HTTP_UPGRADE_INSECURE_REQUESTS] => 1 
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 
    [SERVER_SIGNATURE] => Apache/2.4.18 (Ubuntu) Server at [ip] Port 80
    [SERVER_SOFTWARE] => Apache/2.4.18 (Ubuntu) 
    [SERVER_NAME] => [ip] 
    [SERVER_ADDR] => 172.31.43.105 
    [SERVER_PORT] => 80 
    [REMOTE_ADDR] => 189.208.87.127 
    [DOCUMENT_ROOT] => /var/www/html 
    [REQUEST_SCHEME] => http 
    [CONTEXT_PREFIX] => 
    [CONTEXT_DOCUMENT_ROOT] => /var/www/html 
    [SERVER_ADMIN] => webmaster@localhost 
    [SCRIPT_FILENAME] => /var/www/html/ProyectoPM/Servicios.php 
    [REMOTE_PORT] => 5672 
    [GATEWAY_INTERFACE] => CGI/1.1 
    [SERVER_PROTOCOL] => HTTP/1.1 
    [REQUEST_METHOD] => POST 
    [QUERY_STRING] => 
    [REQUEST_URI] => /ProyectoPM/Servicios.php 
    [SCRIPT_NAME] => /ProyectoPM/Servicios.php 
    [PHP_SELF] => /ProyectoPM/Servicios.php 
    [REQUEST_TIME_FLOAT] => 1510846404.812 
    [REQUEST_TIME] => 1510846404 ) 1 

This is in the array it returns with <?=var_dump($_SERVER); ?>

 ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" 
 ["REQUEST_METHOD"]=> string(4) "POST" 
 ["QUERY_STRING"]=> string(0) "" 
 ["REQUEST_URI"]=> string(25) "/ProyectoPM/Servicios.php" 
 ["SCRIPT_NAME"]=> string(25) "/ProyectoPM/Servicios.php" 
 ["PHP_SELF"]=> string(25) "/ProyectoPM/Servicios.php" 
 ["REQUEST_TIME_FLOAT"]=> float(1510847672.582) 
 ["REQUEST_TIME"]=> int(1510847672) } 

A more important edit At first I said that a simple echo "can you print this"; worked in the host, now I see that it doesn't either. When I move to Servicios.php in by clicking my button in index.html the browser moves to Servicios.php (it displays it in the url) but it simply doesnt show anything. It only shows something if i delete all the code an put a instrucion like <?=print_r($_SERVER);?> which result I already put above.

Sven
  • 69,403
  • 10
  • 107
  • 109
Quique
  • 43
  • 6

2 Answers2

0
if(isset($_POST['Test'])){
echo 'Hello world';
}

This should work. This is checking is button is submitted using POST method. This should be enough check to see if form has been submitted.

Suman B
  • 86
  • 1
  • 6
0

You can try this to check if post method

if(strtoupper($_SERVER['REQUEST_METHOD']) === 'POST') {
 // if form submitted with post method
    // validate request, 
    // manage post request differently, 
    // log or don't log request,
  // redirect to avoid resubmition on F5 etc
}
  • 1
    There's no need to make it uppercase ([this answer shows why](https://stackoverflow.com/a/10766285/6651287)), and if making the check with _equal_ doesn't work, using _identical_ is not going to work either... – Ricardo Ribeiro Nov 16 '17 at 15:50