Using PHP within JS for $redirectlocation and .$_POST

I’ve looked and my tweaks won’t work (noob) but I’ve tried!

I have a custom login for cPanel Webmail at a secured port and would like the error message to populate at the custom login page rather than redirecting to the default ports generic login page.

Read More

Here’s the code I found which should work perfectly except I’m using WordPress and experiencing the headers already sent error. I tried to include the pluggable.php as this has worked with plugins I’ve written in the past for WordPress admin area, but I cannot get the error resolved public frontend.

Username: <input type="text" name="user" value="" size="20" /><br />
Password: <input type="password" name="pass" value="" size="20" /><br />
<div style="display:none">
<input type="text" name="domain" value="emajenweb.webserversystems.com"/>
<select name="port">
  <option selected="selected" value="2096"></option>
</select>    
</div>
<?php
        if($_POST['domain'] && $_POST['user'] && $_POST['pass'] && !($_GET['failed'] == "1")) {
            $port = $_POST['port']; // sets the port number to login to
            // Get the protocol to use for this connection
            switch($port) {
              case '2096': // Secure Webmail
                $protocol = 'https://';
                break;
            }
          // Build the URL
          $redirectlocation = $protocol.$_POST['domain'].':'.$port.'/login/?user='.$_POST['username'].'&pass='.$_POST['pass'].'&failurl='.$_POST['failurl'];
          header ("Location: ".$redirectlocation); // Send URL
        } else {
          $error = 1;
          header ("Location: ".$_POST['failurl']); // Send URL if all neede information is not provided
        }
        ?>

The code I have problems with is:

header ("Location: ".$redirectlocation); // Send URL
        } else {
          $error = 1;
          header ("Location: ".$_POST['failurl']); // Send URL if all neede information is not provided

Given the header already sent error, I am hoping to change the header location values to javascript but I fail…

I have the form working but not as desired with failure messages propagating at the custom login page.

Webmail Login

Related posts

Leave a Reply

1 comment

  1. Error header already sent is a pretty common error. This is caused by… well… header already sent. Header is sent when you send response to server. In your case, it is the:

    Username: <input type="text" name="user" value="" size="20" /><br />
    Password: <input type="password" name="pass" value="" size="20" /><br />
    <div style="display:none">
    <input type="text" name="domain" value="emajenweb.webserversystems.com"/>
    <select name="port">
      <option selected="selected" value="2096"></option>
    </select>    
    </div>
    

    So… how to fix this? Very simple. Just move your processing script BEFORE you send response:

    <?php
            if($_POST['domain'] && $_POST['user'] && $_POST['pass'] && !($_GET['failed'] == "1")) {
                $port = $_POST['port']; // sets the port number to login to
                // Get the protocol to use for this connection
                switch($port) {
                  case '2096': // Secure Webmail
                    $protocol = 'https://';
                    break;
                }
              // Build the URL
              $redirectlocation = $protocol.$_POST['domain'].':'.$port.'/login/?user='.$_POST['username'].'&pass='.$_POST['pass'].'&failurl='.$_POST['failurl'];
              header ("Location: ".$redirectlocation); // Send URL
            } else {
              $error = 1;
              header ("Location: ".$_POST['failurl']); // Send URL if all neede information is not provided
            }
    ?>
    Username: <input type="text" name="user" value="" size="20" /><br />
    Password: <input type="password" name="pass" value="" size="20" /><br />
    <div style="display:none">
    <input type="text" name="domain" value="emajenweb.webserversystems.com"/>
    <select name="port">
      <option selected="selected" value="2096"></option>
    </select>    
    </div>
    

    You should refer to this SO thread for more information: How to fix “Headers already sent” error in PHP