Not able to redirect to next page - php

I am using Win XP os and XAMPP. I was using eclipse as the editor. In Eclipes I was not able to redirect next page so now I have installed Zend Development Environment.
Now also I am getting the same problem.
My Code is
HomePage.php
<html>
<body>
<form name="Form1" id="FormId" action="Welcome.php" method="post">
name : <input type="text" name="txtName">
Phone Number : <input type="text" name="txtPnum">
<input type="submit" name="SubmitIt" value="Submit It">
</form>
</body>
</html>
And Welcome.php is
<?php
ob_start();
session_start();
if(!($_SESSION['UName']))
{
$_SESSION['UName']=$_POST['txtName'];
}
if(!($_SESSION['Ph Num']))
{
$_SESSION['Ph Num']=$_POST['txtPnum'];
}
?>
<html>
<body>
Welcome <?php
if(isset($_SESSION['UName']))
{
echo $_SESSION['UName'];
}
else
{
echo "Session not set<br/>";
echo "{$_SESSION['UName']}";
echo "The session contains <br>";
print_r($_SESSION);
}
?>
</body>
</html>
Its working fine (redirecting to next page) in the Browser but its not working in the debug mode. Both in Eclipse and Zend Development Environment.
Instead of show the content of the next page, it showing the page name.(Welcome.php in my example).
Should I need to install any other extra softwares or code itself worng.... Whats the problem. Please suggest me.
Thanks in advance....!

which part is supposed to make a redirection, i don't see any header('Location: redirect.php') or something
and why do you use ob_start() here .
you didnt release the output buffer add ob_get_clean(); in the end
<?php
ob_start();
session_start();
if(!($_SESSION['UName']))
{
$_SESSION['UName']=$_POST['txtName'];
}
if(!($_SESSION['Ph Num']))
{
$_SESSION['Ph Num']=$_POST['txtPnum'];
}
ob_end_flush();
?>
<html>
<body>
Welcome <?php
if(isset($_SESSION['UName']))
{
echo $_SESSION['UName'];
}
else
{
echo "Session not set<br/>";
echo "{$_SESSION['UName']}";
echo "The session contains <br>";
print_r($_SESSION);
}
?>
</body>
</html>

try to add this at the end of your code i am pretty sure it is because you are not releasing the output buffer, although i think it should have done it automatically
echo ob_get_clean();
Update:
I am not really sure why you are using the $_SESSION variable here, but is you want to fix the problem, you can use for example $uname instead of $_SESSION['UName'];
Welcome.php
<?php // at the beginning of your file, no spaces or newline
session_start();
$uName=$_POST['txtPnum'];
$txtPnum=$_POST['txtPnum'];
$_SESSION['UName'] = $uName;
$_SESSION['PhNum'] = $uName;
?>
<html>
<body>
Welcome <?php echo $_SESSION['UName']; ?>
</body>
</html>
you get rid of the ob start since you are still debugging your code. and try one step at a time.
Wish you good look.

Related

Session block not working

I am new to programming and am experimenting with sessions. I believe the code I have written is correct but after spending time trying to wrap my head around the concept, I am not able to figure out why the program isn't working.
When I debugged the code in the session-page.php, control goes to if(isset...) but then instead of entering the code block or showing a submit button on the browser, simply moves to the next session variable. Someone please be kind enough to explain it to me why this thing ain't working.
Also, can I not use <form> and simply use isset($_GET[ ])?
session-page.php[CODE]
<?php
session_start();
if(isset($_POST['sub']))
{
$_SESSION['xyz']="Hello World";
}
$_SESSION['abc']="Hey Buddy!";
?>
<form method="post">
<input type='submit' name='sub' value='redirect'>
</form>
test-page.php[CODE]
<?php
session_start();
if($_SESSION['xyz']!="Hello World")
{
header("location:session-page.php");
}
echo $_SESSION['abc'];
?>
You want to have this type of workflow (based on what you currently have):
/session-page.php
<?php
session_start();
if(!empty($_POST['sub'])) {
$_SESSION['xyz'] = "Hello World";
header('Location: test-page.php');
exit;
}
if(empty($_SESSION['abc']))
$_SESSION['abc']="Hey Buddy!";
?>
<form method="post" action="#">
<input type='submit' name='sub' value='redirect'>
</form>
/test-page.php
<?php
session_start();
if($_SESSION['xyz'] != "Hello World") {
header("location:session-page.php");
exit;
}
echo $_SESSION['abc'];
?>
try this.
<form method="post" action="" >
<input type='submit' name='sub'value='redirect'>
</form>

Cookies vs Sessions | I get different result

What I want to achieve,
The user enters the one_page.php and we require('form.php') for user to fill while $_SESSION['foo'] isn't set.
The user submits the form and a $_SESSION['foo'] is set.
We header ('Location: one_page.php') (practically like reloading)
We get in the if we unset($_SESSION['foo']) and we require('something_else.php').
If the user reload the site.php or re-enter it he's going to get the form.php again.
I will not show you the actual code because it's too big and I don't want to paste only parts of it but I reproduced the problem at two examples bellow.
Using cookies the code were running exactly as intended.
Using session it's like we get in this if we unset($_SESSION['foo']) but then we leave the if and get into else.
When I set the session for example at page1.php and redirect the user to page2.php to unset the session everything seems fine. I just can't get it work when I create the session at the same page where I unset it.
Examples
Using Cookies We get in the IF when we press the button
<?php
if (isset($_POST['submit'])) {
setcookie('foo', 'foo', time() +3600);
header('Location: one_page.php');
}
if (isset($_COOKIE['foo'])) {
setcookie('foo', 'foo', time() -3600);
echo "We entered the IF"; //require('something_else.php')
} else {
echo "We entered the ELSE"; //require('form.php')
}
?>
<!-- The form which is required in my case -->
<html>
<body>
<form method="post">
<button name="submit">Button</button>
</form>
</body>
</html
Using Sessions We are in the ELSE no matter what
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['foo'] = "foo";
header('Location: one_page.php');
}
if (isset($_SESSION['foo'])) {
unset($_SESSION['foo']);
echo "We entered the IF"; //require('something_else.php')
} else {
echo "We entered the ELSE"; //require('form.php')
}
?>
<!-- The form which is required in my case -->
<html>
<body>
<form method="post">
<button name="submit">Button</button>
</form>
</body>
</html>
I could just use cookies but this is bugging me so much for hours now.
Any thoughts?
Adding exit() just after the header('Location: one_page.php') fixed the problem.
The unset($_SESSION['foo']) were running before the redirection as Dagon said.
Fixed
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['foo'] = "foo";
header('Location: one_page.php');
exit();
}
if (isset($_SESSION['foo'])) {
unset($_SESSION['foo']);
echo "We entered the IF"; //require('something_else.php')
} else {
echo "We entered the ELSE"; //require('form.php')
}
?>
<!-- The form which is required in my case -->
<html>
<body>
<form method="post">
<button name="submit">Button</button>
</form>
</body>
</html>
Thank you Dagon!

php script is printed not executed

I have following html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fancy Website</title>
</head>
<body>
<div id="content_login">
<form method="post" action="app.php">
<table>
<thead>Please Login for more fancy Content!</thead>
<tr>
<td>Username</td>
</tr>
<tr>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td><input type="submit" name="login" value="Login"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
And this is my php script:
<?php
if(isset($_POST['login'])) {
echo "Hello " . $_GET["username"];
}
?>
Yeah simply nothing, but I only wanted to test, if a script would work when the Login Button is pressed. Surprise: It's not. I open index.html in my browser and the html part works properly, but if I press the Login Button the browser shows me this:
<?php
if(isset($_POST['login'])) {
echo "Hello " . $_GET["username"];
}
?>
I guess it's a syntax issue but I can't find it. Maybe you see it.
Thanks a lot!
My advice would be to use:
<?php
if (isset($_POST['login']))
{
$username = $_POST['username'];
if (!empty($username))
{
echo "hello $username";
} else {
echo "You must fill in the username!";
}
}
?>
To be honest, I would change index.html to index.php and place it into the top of that page so all errors etc are passed through one file.
you should change that $_GET["username"] to $_POST["username"] variable to work it correctly
<?php
if(isset($_POST['login'])) {
echo "Hello " . $_POST["username"];
}
?>
Do you have the php code in app.php? If you have then you have not enabled mod_php in Apache. I would recommend you to use wampserver (on windows) since that takes care of the basic apache/php configuration.
http://www.wampserver.com/en/
EDIT: You can not just execute your php code without having a webserver. Opening HTML files in your browser is client side and php is server side.
First I think you don't have apache server running to make your PHP code works correctly.
And you are sending the data using POST no GET, you must use the POST method to handle the data.
<?php
if(isset($_POST['login'])) {
echo "Hello " . $_POST["username"];
}
?>

Session variables not passing to next file

So I've searched this site about this issue and tried what has been suggested and still no luck. I thought maybe it was my 'server' (On my tablet using KSWEB, no computer right now) so I created 2 simple files to share a session variable between the two and it worked fine. I have no idea why this isn't working for these two. I'm trying to create a login page (an insecure one, I know). The error function USED to work (this is what gets me), and now it doesn't. The files are below. I only included the top portion of admin.php because I've commented out the rest. It really shouldn't matter. Right now, if you submit the form without entering anything into the admin prompt, an error should display next to the asterisk saying "Admin needs to be filled out". Despite my best efforts, this doesn't work anymore and I'm completely stumped as to why.
Login.php
<?php
session_start();
?>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
<script>
function submitForm()
{
document.adminform.submit();
}
</script>
</head>
<?php echo $_SESSION["adminErr"];?>
<h2>Administrator login page</h2>
<form method="post" action="admin.php" name="adminform">
Admin: <input type="text" name="admin" style="position:absolute; left:100px">
<span class="error" style="position:absolute; left:285px">*<?php echo $_SESSION["adminErr"];?></span>
<br><br>
Password: <input type="password" name="password" style="position:absolute; left:100px">
<span class="error" style="position:absolute; left:285px">*<?php echo $_SESSION["passwordErr"];?></span>
<br><br>
<button onclick="submitForm()">Submit</button>
</form>
<br><br><br>
<p><?php echo $_SESSION["flogin"];?></p>
</html>
<?php
session_destroy();
?>
Admin.php
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == POST)
{
if (empty($_POST["admin"])) // Check to make sure Admin field is filled out
{
$_SESSION["adminErr"] = "Admin field must filled"; // Set error if not filled
header("location:login.php"); // Return to login page
}
}
?>
Don't destroy the session at the end of the file..
</html>
<?php
session_destroy();
?>
Also you should put exit; after each header('Location: ...');.
When sending the header, the browser recognized to change the location but the script does not end. The browser in fact, would not even have to follow the header, it can also just go on with the script. You have to stop the script because the headers do not exit the script.
instead of
<button onclick="submitForm()">Submit</button>
use
<input type="submit" value="Submit">
Then put a check before echoing
<?php echo isset($_SESSION["adminErr"])? $_SESSION["adminErr"]: "not set" ;?>
further debugging:
var_dump($_POST);
var_dump($_POST["admin"]);
var_dump($_SESSION);
var_dump($_SESSION["adminErr"]);

php form never shows anything whenever it's submitted

<html>
<body>
hi!
<?php
Hi!
if(htmlspecialchars($_POST["name"] == "hey"))
{
Hi!
}
?>
</body>
</html>
It's probably something small, but I can't figure out why my message never shows when I try running it. I tried echo, print, and just typing the text out on screen, and I can never get the php form to run, it's always blank. Perms are set to 644. The form submitting the block of code's below...
<html>
<body>
<form action="action.php" method="post">
<input type="text" name="name"><br>
<input type="submit">
</form>
</body>
</html>
In addition to the comments and answers from other users regarding your code missing an echo or print() on "Hi", your brackets are mixed up:
if(htmlspecialchars($_POST["name"] == "hey")) should be :
if (htmlspecialchars($_POST["name"]) == "hey")
You have a syntax error, to print data within php you need to use echo:
<?php
echo "Hi!"; // <--- here
if(htmlspecialchars($_POST["name"]) == "hey") // <---- here you was a syntax error too
{
echo "Hi!"; // <--- here
}
?>
Or other related functions like: print, print_r, var_dump
you couldn't just write html inside php without echoing it..
<html>
<body>
hi!
<?php
if(htmlspecialchars($_POST["name"]) == "hey")
{
echo "Hi!";
}
?>
</body>
</html>

Categories