Mohamed Mansour's Personal Website
Articles
How to redirect to a website with POST data ASP PHP
Posted on January 20, 2009, 11:04 pm EST
There are some situations where you would need to redirect to a webpage with post parameters being sent using any web scripting language such as PHP, ASP, JSP, etc ... Sure you can download a webpage with post request, but that doesn't mean redirection. I have done this today to help out a colleague, and would like to share some of the problems that came in the way, and hope it will help anyone that has problems.
Basically, we had to redirect to a Dot Net Nuke (DNN) page so that we could log in a browser control within a Java application. DNN login component uses POST requests. Some Java browser components such as JDIC can redirect to a page with POST but that library is not reliable, we were using other browser libraries which does not allow the user to navigate to a page with post parameters (since its not implemented in SWT Browser). Even if the browser libraries allow us to send post redirects, it still wont solve our main problem. Our main problem was to redirect to a POST page and set some session variables while navigating to that page. Therefore, this proposed idea works perfect.
Many people online recommended creating a dummy HTML page that will use some basic javascript to submit the form. That idea works great. I will explain how that works in detail with a working example including setting session variables while redirecting. The main files that you should look at would one of these, depending if you want to do it in PHP or ASP.NET " redirectform.php" or " redirectform.aspx"
I have done a simple example that will show the process. The same would be applied to any page, like a Dot Net Nuke page. The page which we want to redirect to with post looks like the following:
CODE:
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Please Login!</h1>
<form name="login" action="login.php" method="post">
<input type="text" name="username" value="" />
<input type="password" name="password" value="" />
<input type="submit" name="submit" value="Login" />
</form>
</body>
</html>
login.php - This is just a simple page where it will echo out to the screen if a post has been submitted.
CODE:
<?php
session_start();
if (isset($_POST['username']) && isset($_POST['password'])) echo 'Logged In';
else echo 'Not Logged In';
if (isset($_SESSION['somesessionvar']))
echo 'Session: ', $_SESSION['somesessionvar'];
?>
redirectform.php - A basic form that when you open it, it will redirect directly to the post.php using POST parameters.
CODE:
<?php
session_start();
$username = isset($_GET['username'] ? $_GET['username'] : '';
$password = isset($_GET['password'] ? $_GET['password'] : '';
$_SESSION['somesessionvar'] = 'testing';
?>
<html>
<head>
<script type="text/javascript">
function doPost() {
document.getElementById("TestForm").submit();
}
</script>
</head>
<body onload="doPost()">
<form id="TestForm" action="login.php" method="post">
<input type="hidden" name="username" value="<?=$username?>" />
<input type="hidden" name="password" value="<?=$password?>" />
</form>
</body>
</html>
A similar one in ASP.NET / ASP could be the following redirectform.aspx. In this example, will use aspx, converting this to classic asp is relatively simple, just remove the types, and Dim to dim.
CODE:
<%
Dim username As String = Request.QueryString("username")
Dim password As String = Request.QueryString("password")
Session("somesessionvar") = "testing";
%>
<html>
<head>
<script type="text/javascript">
function doPost() {
document.getElementById("TestForm").submit();
}
</script>
</head>
<body onload="doPost()">
<form id="TestForm" action="login.php" method="post">
<input type="hidden" name="username" value="<% Response.Write(username) %>" />
<input type="hidden" name="password" value="<% Response.Write(password) %>" />
</form>
</body>
</html>
That is it, you will just need to redirect to that redirection page and it automatically let javascript to submit that invisible form.
CODE:header('http://localhost/redirectform.php?username=m0&password=123')
Or in ASP.NET
CODE:Response.Redirect("http://localhost/redirectform.aspx?username=m0&password=123'")
Thats all, it solved my problems, and I hope it solves yours.

