PHP link builder to avoid use of Java Script and hide the old URL

tryingtogethelp

I'm creating a login page that accepts a username and then redirects the user to log in. This is currently done, and works with the following Java Script.

function process()
{
var url="https://example.com/users/profile" + document.getElementById("username").value;
location.href=url;
return false;
}
<p>Enter your username</p>
<form onsubmit="return process();">
<input type="text" name="username" id="username">

I'd prefer to do this without using Java Script to support users that disable it, older browsers and to hide this from the source code of the page. The subdirectories are protected anyway but I just want the added compatibility.

I'm attempting to do this with PHP instead;

<form action="/authenticate.php">
<input type="text" name="username" id="username">

I'm using the same form but have created a page called authenticate.php instead. All that authenticate.php contains is;

<p>Authenticating…</p>
<?php 
$username = ["username"];
header("Location: https://example.com/users/profile/$username"); die();
?>

If steve was the input, I'd expect that to then redirect to https://example.com/users/profile/steve but it doesn't. I've set up redirects already to handle errors and the form translates text to lowercase anyway.

I'm just wondering why;

<?php 
    $username = ["username"];
    header("Location: https://example.com/users/profile/$username"); die();
    ?>

won't work with the addition to the URL but does work without the $username so that's the only error. I also tried $username = $POST_["username"]; but that's not relevant and doesn't seem to work either. The current code takes me to https://example.com/users/profile/Array

If someone could advise on the correct way to do this I'd very much appreciate it.

Kamran KB

By default form method is GET but the best practice is to mention it so you've to do:

<form action="/authenticate.php" method="get">
   <input type="text" name="username" id="username">
</form>

And authenticate.php you need to get input value:

<p>Authenticating…</p>
<?php 
  $username = $_GET["username"];
  header("Location: https://example.com/users/profile/$username");
  die();
?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related