i am facing a problem using the php header() function. I am trying to redirect to another page after form submission. The main thing is, that it works fine on my local XAMPP server, but not on my web server hosted by strato..
I have already searched the web for solutions but not a single one applies to my problem. So there is no whitespace before my php tag, i do not ouput anything before i call the header function and there is also nothing included that can cause any problems. I've also tried to use absolute pathes to state the page i would like to redirect to. No result..
Here is the whole content of my file redirect.php
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
if (isset($_POST["submit"])) {
// redirect
header("Location: register.php"); exit;
//echo "<script language='javascript'>window.location.href='register.php';</script>";
}
?>
<!doctype html>
<html>
<head>
<title>Redirect</title>
</head>
<body>
<form action="redirect.php" method="post">
<button type="submit" name="submit">Submit</button>
</form>
</body>
</html>
There is no error output on the page, instead it will stay completly blank after form submission..
The redirection via javascript is working, but is just a workaround since it will not work if javascript is disabled.. But it shows that the if-statement will be reached!
Any suggestions?
Related
I'm trying to execute a shell script from an HTML page using PHP. I have found a previous example here that I have been trying to follow but I'm having an issue. I'm not sure what the cause is but no errors are returned and no file is created from the bash script.
index.php:
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form action="./test.php">
<input type="submit" value="Open Script">
</form>
</body>
</html>
test.php
<?php
exec("./bash_script.sh");
header('Location: http://local.server.edu/ABC/abc_test/');
?>
bash_script.sh
#!/bin/bash
touch ./test_file.txt
One thing I have noticed that may be the cause of the issue is that it seems the path on the local server doesn't match with the file system exactly.
If I switch all the relative paths in the scripts to absolute paths such as:
/local/sequence/temp/abc_test/file.exe
Then after clicking the button to run the script I get an error saying:
The requested URL /local/sequence/temp/abc_test/test.php was not found on this server
Edit:
The three files They're located at /local/sequence/temp/abc_test And there is a symbolic link pointing to that directory at /export/www/htdocs/ABC
The error message seems to be indicating that test.php is not being found. As written, it needs to be in the same directory as index.php
You’ve tested the actual bash script, so we can proceed with the assumption that it’s in the execution of the script receiving the submission.
I would suggest putting all the web stuff into one page, because you can test sending and receiving input.
<?php
// for testing
// exec("./bash_script.sh");
// check for POST submission (this is not just reading data)
if(isset($_POST['runScript'])) {
// die('Request received');
exec("./bash_script.sh");
// It’s always proper to redirect after post :)
header('Location: http://local.server.edu/ABC/abc_test/');
die;
}
// finished with logic; show form
?>
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form method="POST">
<input type="submit" name="runScript" value="Open Script">
</form>
</body>
</html>
Note that I added the name attribute to the submit button, and made the form use POST method while submitting to the calling page (no action means submit to yourself).
I’ve also left a few commented actions to aid in debugging if necessary
You may have to adjust the path to the bash script. Currently it’s going to look in the same directory as index.php, which is not something you’d want to do in production.
You will be able to do that somehow, but its always very risky to allow such operations to execute from the php page.
I have an issue with my form.
Here's my form structure.
<form method="POST" action="../login.php">
<input type="submit" name="login" value="test">
</form>
It's really only that.
But on my login page, it cannot get the POST parameter, and error logs says nothing.
<?php
if(isset($_POST['login']))
{
echo "true";
}
else {
echo "false";
}
^^ That's my php code, right there. I've also tried with
if($_SERVER['REQUEST_METHOD'] == "POST"){}
Instead of isset.
Can anyone see what i do wrong here??
Kind regards
open login.php directly from URL and at the top of the code in login.php add the code given below.
<?php
echo "hello";die;
?>
if you see the hello on the page that means your login.php working fine. but if you not that means your file is renamed incorrectly sometimes it happens by our editor. the file becomes like login.php.txt
If the filename is correct and you are not getting hello then your server is not working.
and it seems your code is also fine.
You can also use POSTMAN to post data on your page without form and there is one more alternative ajax.
../login.php is the wrong path.
if everything works fine then the error is 100% in the path.
I am attempting to load a webpage on my own server which will run a .bat script (on the same server) as below.
When I access the page, called test.php, it display the 'DO IT!' button and when I press it, it just display the content on the .bat file rather than executing it on the server...
What do I need to configure on the server, I assume in the PHP settings, to force it to run the script rather than just display it on the webpage?
For the purpose of the question, I am happy about the security implications of what I am doing.
I am running a Windows machine with IIS and PHP.
<html>
<head>
<title>Restarting</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
echo exec('c:\scripting.bat');
echo "Done!";
} else {
// display the form
?>
<form action="" method="post">
<input type="submit" name="submit" value="DO IT!">
</form>
<?php
}
?>
</body>
</html>
I think that the echo exec('c:\scripting.bat'); line it's causing you the problem. Try to just execute it without the echo statement.
If you trying to see the output of the function, you must use the second functions parameter: &$output, acording to the documentation itself. See it in the docs here.
I hope it will be useful to you! :D
I am trying to change the page when the user hits the login button. When the login button is hit currently the page just refreshed the user is not redirected to the new page. I created the session before any of the code for the page. I am wondering if it has to do with the location of my header command.
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
</head>
<body>
<form action="" method="post">
<br>Name: <input type="text" name="NameTextBox"><br>
<br>What grade are you currently in?: <input type="number" name="GradeTextBox"><br>
<br><button name="Login" type="submit">Login</button></br>
</form>
<?php
if (isset($_POST["Login"])) {
$_SESSION["start"] = $_SERVER['REQUEST_TIME'];
//echo "This session is beginning at ".$_SESSION["start"]."<br /><br />";
}
if(isset($_POST["Login"])){
if($NameTextBox = "Phydeaux"){
//echo"Good Name!";
PassLogin();
}
elseif($NameTextBox = "Rover"){
//echo"Good Name!";
PassLogin();
}
elseif($NameTextBox = "Spot"){
//echo"Good Name!";
PassLogin();
}
else{
echo"You cannot login!";
}
}
function PassLogin()
{
//print '<script type="text/javascript">';
//print 'alert("Running PassLogin Function")';
//print '</script>';
$_SESSION["ReadingGrade"] = "Fail";
$_SESSION["WritingGrade"] = "Fail";
$_SESSION["MathGrade"] = "Fail";
$_SESSION["Grade"] = $GradeTextBox;
$_SESSION["Name"] = $NameTextBox;
Header('Location: Reading.php');
}
if (isset($_POST["Login"])){
//echo "Login has been pressed";
}
?>
</body>
</html>
There are several synthax and technical problems with this code. In addition to what AlexP said, you must observe that you cannot send the headers when you have already sent some output. The headers must be sent before any output, what is the same reason you had to put your session_start() in the beginning.
That being said, you should also make the login script before the HTML part, where your session_start() is, so your redirect header to Reading.php can work too. Or you can workaround that by placing a ob_start() in the beginning of the page, it will buffer the output preventing it from being sent before the script ends, this way you can call a header() wherever you like.
And talking about the header, the function is header(), not Header(). There is no native function named Header() in PHP. Function names are case sensitive.
In addition, you shouldn't use action="" in your <form>. Just suppress this attribute to make it post to the same page, it will make your code work don't matter what your php file name is. If you use action="" it will post to the index.php instead of login.php.
I also noticed you are using Reading.php (with capital letter) and login.php. I suggest you normalize all your file names to lower case, because if not you can have problems when porting this code to other systems. To Windows, Reading, reading, READING and ReAdInG are the same thing, to Linux it is not.
You cannot modify the header information having already output your HTML. Move your HTML underneath the PHP header call.
The documentation says:
header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP
Also, you are assigning the string value when you use $NameTextBox = "Phydeaux", rather than conditionally checking it.
You should use the == double equals instead
if(isset($_POST["Login"])){
$NameTextBox = $_POST['NameTextBox']; // $NameTextBox is now the posted value
if($NameTextBox == "Phydeaux"){ // double equals checks the value of the variable
<form action="" method="post">
Where will go your posts?
I will offer you this:
1- Write your PHP codes another page and named is "login.php"
2- Write your form action: <form action="login.php" method="post">
I have a page with simple form. when I click on Submit button, i have some sql codes and at the end I need to redirect page to another page(contact.php).My sql codes are working fine and it stores in database, however it does not redirect to another page(to index.php) and shows the same page which has form(contact.php).
I use the following code to redirect my page:
header('Location: index.php');
my form and php codes are as below:
<form name="contact" action="contact.php" id="contact_form" method="post" >
<input type="hidden" id="ID" name="ID" value="<?php echo $yid ?>" />
<textarea id="Reason" name="Remark" placeholder="Write your Reason here" class="required" cols="10" rows="10"></textarea>
<input class="button altbutton" type="submit" name="submit" value="submit" />
</form>
if (isset($_POST['submit'])) {
$Remark = #$_POST ['Remark'];
$ID= #$_POST ['ID'];
$query=" my sql code";
$result = mysql_query($query);
header('Location: index.php');
}
what should I do?or what I have missed? Thank you
I'd imagine it's cause you have put it after output on the page, but without seeing your code this is just a stab in the dark.
To redirect to a new page you should use:
header("Location: index.php");
exit();
Also, make sure the above is placed before any output on the page, otherwise it won't work.
Doing the above, if you get an empty page, something has went wrong (check your error log or make sure error reporting is turned on).
is there some html sent before the header? even whitespace?
also try this (from here)
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
if(isset($your_set_values)
{
header("Location:index.php");
}
if it doest seem to work :-
First try to find is there any error ?
using --
error_reporting(E_ALL & ~E_NOTICE);
if you didn't found any error .... try to print your query and what..
The HTTP header "Location" don't seem to work properly always on all browsers (as per my experience). And if you have already 'echo'-ed some HTML (or text, whatever) before echoing the 'Location' header, the PHP function 'header' won't work.
Use this code:
<?php
$Js_Redirect =
"<script>"+
"top.location = 'index.php';"+
"</script>";
echo $Js_Redirect;
exit();
?>
instead of
<?php
header('Location: index.php');
?>
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
?>
[reference]
Solution 1: In your file, move the PHP code to the begining (i.e. above <html> tag), and also do not echo anything before using header() function.
Solution2:
If you're having trouble to refactor your code, then you need to use Output buffering. Put ob_start() as the very first line of your php page, and use ob_end_flush() or ob_end_clean() after use of the header(). This effectively buffers all output, which allows you to call the header function before any output is actually printed.
Note: remember to always exit or die() after setting a Location header.