Getting PHP SESSION to work [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Hello everyone first of all I have done a lot of research in the forum and other pages during these whole weekend before finally giving up and posting it.
Here is the code:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>El primer script</title>
</head>
<body>
<div id="wrapper">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>numero: <input type="text" name="number" /></p>
<p><input type="submit" value="start" /></p>
</form>
</div>
<?php
if(!isset($_SESSION['CREATED'])) {
session_start();
$_SESSION['aux'] = 1;
} else {
$_SESSION['aux'] = 0;
}
if(isset($_SESSION['CREATED'])){
var_dump($_SESSION['aux']);
}
?>
</body>
</html>
What I want to accomplish is that when I press the button (doesn’t matter if input is empty or not) it shows the number 1 but if I press again doesn't matter how many times it shows 0.
It is not working, it always shows 1, I don't think i managed to make the session work at all.
I might have mistaken the functionality of something rather than making a syntax error. I am beginning with php so if anyone can help if would be appreciated.

Whenever you use the global $_SESSION you have to declare session_start() at the very top of the page.
Try Using:
<?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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>El primer script</title>
</head>
<body>
<div id="wrapper">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>numero: <input type="text" name="number" /></p>
<p><input type="submit" value="start" /></p>
</form>
</div>
<?php
if(!isset($_SESSION['CREATED'])) {
$_SESSION['aux'] = 1;
} else {
$_SESSION['aux'] = 0;
}
if($_SESSION['CREATED'])){
var_dump($_SESSION['aux']);
}
?>
</body>

Related

Is it possible to hide code written in php in source code

below is my code where i am writing a code in php and calling it in body. When i run it and view source, the code gets visible. I have seen some site where nothing gets shown on source but a form appears on webpage. How can i do it too?
<?php
{ $avatar_form = '<form id="avatar_form" enctype="multipart/form-data" method="post" action="myphoto.php">';
$avatar_form .= '<h4>Change your avatar</h4>';
$avatar_form .= '<input type="file" name="avatar" required>';
$avatar_form .= '<p><input type="submit" value="Upload"></p>';
$avatar_form .= '</form>';
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Avatar</title>
</head>
<body>
<div><?php echo $avatar_form; ?></div>
</body>
</html>
Thanks
Shail
Why not write it in html? There should not be any code showing like that:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Avatar</title>
</head>
<body>
<div>
<form id="avatar_form" enctype="multipart/form-data" method="post" action="myphoto.php">
<h4>Change your avatar</h4>
<input type="file" name="avatar" required>
<p><input type="submit" value="Upload"></p>
</form>
</div>
</body>
</html>
just using php to echo html is sometimes not faster or useful then writing it directly in .html files.
Check whether the file type is .php or .html?
if html, it will show the php code.
If so, save it as .php

textbox number type in html cut off 0 if post form in php

If i give textbox type as number and submit a form it cut off 0 .
ex: 099921 means it will post as 99921
why it cut off preceding 0
Because 0 in front means nothing and is removed. Make it a string (text) and it will stay. Ints have no 0 in front.
i dont know why but i did the same code and it is displaying the value with o i.e 099921
check out my code
<?php
if(isset($_POST['submit']))
{
echo $numberValue = $_POST['numVal'];
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="post">
<input type="number" name="numVal" value="" />
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html

Why received empty data using $_POST function in php side from the posted HTML form textfield [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php $_POST array empty upon form submission
I just make a simple test to POST a HTML format's textfield data to PHP server. And, in PHP server side, just use the $_POST[] function to retrive the posted data.
However, there is empty(null) data recevied always. Attached below are the HTML code and php script I tested.
Appreaicted if you can point me where I was wrong?
----HTML code---
senddata.html
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>Untitled Document</title>
</head>
<body>
<form action="receivedata.php" method="post" enctype="application/x-www-form-urlencoded" name="form1" id="form1">
<label>_id
<input name="_id" type="text" id="_id" />
</label>
<br />
<label>Send
<input type="submit" name="Submit" value="Send" />
</label>
</form>
</body>
</html>
---PHP Script-----
receivedata.php
<?php
echo $_SERVER['SERVER_NAME']."<BR/>" ;
$id = $_post['_id'];
if ( isset($id))
{
echo "Data set ready!<br/>";
echo "id=".$id."<br/>";
}
else
{
echo "Data set is not ready!<br>";
}
?>
and the result display on the browser:
192.168.0.108
Data set is not ready!
$_POST[] is not a function but language construct used to add items into $_POST array.
While $_POST is a variable, though you are not using it anyway but whatever $_post variable instead.

Checking if URL has been tampered with

I am trying to ensure the parameter from a GET method of page 2 (which displays the result) will not be changed by others in the URL (the variables behind the question mark) by making sure users clicked the submit button on page 1 (which contains the form).
The professor ask us to use isset() to implement this requirement
Here's the login page: (page1.php)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ...> <html
xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Login
Page</title> </head>
<body>
<label>User ID:</label>
<input type="text" name="uid">
<br />
<input type="submit" value="SubmitCredentials" name="SubmitCredentials"/>
<br />
</form> </body> </html>
And this is the second page: (page2.php)
<?php
// get the data from the form
$uid = $_GET['uid'];
if(!isset($_GET['SubmitCredentials'])){
include('error.html');
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional
...>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HELLO</title>
</head>
<body>
<label>Hello,</label>
<span><?php echo $uid; ?></span><br />
</body>
</html>
However, the if statement doesn't seem to be working. It's supposed to forward the page to error.html when I change the variable uid in the URL (e.g. when I change the URL to http://localhost/1/page2.php?uid=admin) but it's not. How can I fix it?
PS: a relevant tutorial I found on google>>>
http://www.homeandlearn.co.uk/php/php4p7.html
Try:
if(!isset($_GET['SubmitCredentials'])){
header('Location: error.html');
}
Isset is not safe on Arrays. Try comparing with the ===operator instead:
if(!($_GET['SubmitCredentials'] === 'SubmitCredentials')) {
include('error.html');
exit();
}
Page #1:
<form method="GET" action="...">
<input>
<input>
<input>
</form>
Submit URL:
http://localhost/1/page2.php?SubmitCredentials=SubmitCredentials&uid=admin&i_like_php=yes&...

PHP not working with Expression Web 3

I'm starting to try to learn PHP while using Expression Web 3. I set up PHP runtime and configured the ini. What happens is my script doesn't do as it should.
My page looks like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-us" http-equiv="Content-Language" />
<title>Sports</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="master.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
</head>
<body>
<div id="masthead">
</div>
<div id="top_nav">
<ul>
<li>Sports </li>
<li>Clubs</li>
<li>Cloud</li>
</ul>
</div>
<div id="container">
<div id="left_col">
</div>
<div id="page_content">
</div>
</div>
<div id="footer">
<form method="post">
</form>
</div>
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
</body>
</html>
Then my PHP file looks like:
<!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>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>action</title>
</head>
<body>
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
</body>
</html>
What should happen is for example if I type John as name and 50 as age, it should output Hi John, you are 50 years old.
But instead it says (with Firefox) :
Hi you are years old
in Internet Explorer 8, it just outputs the whole php script.
I'm not sure where I went wrong.
Thanks
Either you are not running these files from a PHP enabled webserver or the webserver is not configured to serve the used file extension as PHP. How to install and configure a webserver is best asked on ServerFault.com. Have a look at XAMP or Zend Server CE.
In addition, you have a terribly low acceptance rate. Why not go back to some previous questions and you asked and accept some of the answers you have been given?

Categories