use the session in the form - php

<?php
session_start();
$_SESSION['id']='face';
?>
<!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>Untitled Document</title>
</head>
<body>
<form action="check.php" method="post">
Admin: <input type="text" name="uname" value="<?php if(isset($_SESSION['id']) && !empty($_SESSION['id'])) { echo $_SESSION['id']; } ?>" /><br />
Password: <input type="password" name="pword" /><br />
<input type="hidden" name="login" value="1" />
<input type="submit" value="Login" />
</form>
</body>
</html>
I am working in a php language . i have made session[id] in which i have stored the the value ie face . Now in the form , ADMIN textbox , i am checking for the session[id] , if isset or not empty *echo* session[id] but if not isset or empty echo the textbox value instead of this its showing me tha number . please can any one explain me why its showing me the number .......instead of value

First, correct your html as follows:
<input type="text" name="uname" value="<?php if(isset($_SESSION['id']) && !empty($_SESSION['id'])) { echo $_SESSION['id']; } ?>" />
if you get the same error you need to check your session value before displaying the form. You can use:
var_dump($_SESSION);
to make sure if your session is not overwritten somewhere else.

There is no <input type="textbox" in html!
Use <input type="text" or <textarea> </textarea> (content goes in between)
EDIT: Session ID
If you really want to overwrite the Session ID, use the function session_id(sid). Either way, you can only set the session ID before you start the session, so your code should look like this:
session_id("face");
session_start();

Related

Want to store page url to mysql using html form field

I have two php files. index.php and insert.php. I am inserting some data by using form but I also want to store the index.php url into mysql by using html form. Because I have many pages like index.php which are calling insert.php. Actually when some page call insert.php it should take the url of that page by using form so that i can store that url into database. Code is given below.
<?php
$con=mysqli_connect("localhost","root","","commentdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Everything is working fine but not taking the url
$sql="INSERT INTO comment (id, name, email,comment,url)
VALUES
(null, '$_POST[name]','$_POST[email]','$_POST[comment]', $_POST[url])";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
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=utf-8" />
<title>My Comment Box</title>
</head>
<body>
<?php
//getURL function to get URL
function getURL()
{
/* First check if page is http or https */
//$whichprotocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
/* Combine different pieces of $_SERVER variable to return current URL */
//return $whichprotocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
return '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
/*Calling getURL function to display current page URL*/
?>
<form action="insert.php" method="post">
<p>Name:
<input name="name" type="text" />
</p>
<p>Email:
<input name="email" type="text" />
</p>
<p>Comment:</p>
<p>
<textarea name="comment" cols="50" rows="10"></textarea>
</p>
<p>
<--! getURL(); gives me error. It takes url but don't send it insert query -->
<input type="hidden" name="url" id="url" value="<?php getURL();?>" />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
</form>
<br />
<?php
echo getURL();
?>
</body>
</html>
You need to echo the value from the function -
value="<?php echo getURL();?>" />

Pass session values to other page

I have two pages: order.php and checkout.php. I have 3 items in the order page and I want to pass quantity of the items to the checkout page.
I guess the problem is with isset($_POST['Submit']). My guess is that it still goes straight to the checkout page when I press submit without putting values to session variables.
I have been trying to pass the values from order like this:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!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">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
echo <<<END
<form action="checkout.php"
method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" /></label></p>
<p>Frigge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="submit" value="Order"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['Submit'])) {
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
?>
</body>
</html>
And here in checkout I'm trying to print one session value as test:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!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">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
print ($_SESSION["maara1"]);
?>
</body>
</html>
"I guess the problem is with isset($_POST['Submit'])."
Yes, the problem is with if (isset($_POST['Submit']))
Your submit button is named submit instead of Submit.
<input type="submit" name="submit" value="Order"/>
Change it to
<input type="submit" name="Submit" value="Order"/>
They are case-sensitive.
Or leave it the way it is and change
if (isset($_POST['Submit']))
to
if (isset($_POST['submit']))
Either method will work. You just need to make them both (letter cases) match.
What is happening is, PHP is looking for a named element called Submit.
That alone would have and should have thrown:
Undefined index Submit...
Sidenote:
I noticed:
<input type=hidden name=price1 value=5>
and other inputs.
I would suggest that you use quotes around it:
<input type="hidden" name="price1" value="5">
while doing the same for the others, as it could have adverse effects and/or unexpected results.
I have seen that happen before.
Edit:
You'll need to move this whole block in your second page and not be in the first page.
if (isset($_POST['Submit'])) {
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
then do print ($_SESSION["maara1"]); from there.
Your first page does not recognize the POST variables because they have not been set.
Edit #2:
You could try setting a value value="{$_SESSION["maara1"]}" to your inputs.
I.e.:
Sidenote: You could try <form action="" method="post"> instead of <form action="checkout.php" method="post">
However, I'm unsure if the following is what you're looking to get. It does work if action="" but it won't work trying to get a value before it has been set. That's not how sessions work.
It's kind of like expecting an A+ in a test you haven't written yet, if I can say.
echo <<<END
<form action="checkout.php" method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" value="{$_SESSION["maara1"]}" /></label></p>
<p>Frigge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="Submit" value="Order"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['Submit'])){
$_SESSION["maara1"] = $_POST["maara1"];
$mar1 = $_SESSION["maara1"];
echo $mar1;
}
But as you said in a comment: "what is the point of me using session variables on second page then if I can refer them from $_POST anyways?"
A: Exactly.
I went and did it like this. With two button and it is also more close to webstore now. So I press first button to add the quantity of the items and at the same time it sets session variables. Then I press second button to proceed to the second page which is checkout. What you guys think?
First page:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!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">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
echo <<<END
<form action="teht7.php"
method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" /></label></p>
<p>Fridge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="submit" value="Valitse tuotteet"/></p>
</form>
<hr />
<form action="teht7_kassa.php"
method="post">
<p><input type="submit" name="submit" value="Siirry kasssalle"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['submit'])) {
$ostostenmaara = $_POST["maara1"] + $_POST["maara2"] + $_POST["maara3"];
print ("Ostoskorissa on: $ostostenmaara tuotetta");
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
?>
</body>
</html>
Second page:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!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">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
$maara1 = $_SESSION["maara1"];
$maara2 = $_SESSION["maara2"];
$maara3 = $_SESSION["maara3"];
$summa = $_POST["price1"]*$maara1+$_POST["price2"]*$maara2+$_POST["price3"]*$maara3;
print ("Ostostesi yhteissumma on: $summa euroa");
?>
</body>
</html>

php sessions to carry over to subdomains dont work

I just read this link and try to implement it Allow php sessions to carry over to subdomains
in my localhost I have the following index.php file
<?php
ini_set('session.cookie_domain', '.localhost' );
if ("" === $_POST['login_name']){
}elseif ("" === $_POST['login_password']){
}else{
$_SESSION['login'] = true;
$_SESSION['user'] = $_POST['login_name'];
$_SESSION['group'] = 'support';
}
?>
<!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>Idea Networks And Communications Limited</title>
</head>
<body>
<form action="" method="post" style="display:block;margin:0 auto">
<fieldset>
<legend>Please, Log In</legend>
<label >User Name</label>
<input name="login_name" type="text" maxlength="32" />
<br />
<label>Password</label>
<input name="login_password" type="password" maxlength="32" />
<br />
<input type="submit" value="Log In"/>
</fieldset>
</form>
</body>
</html>
<?php
echo var_dump($_SESSION['login']);
echo var_dump($_SESSION['user']);
echo var_dump($_SESSION['group']);
?>
In subdomain.localhost I have the following index.php file
session_start();
echo var_dump($_SESSION['login']);
echo var_dump($_SESSION['user']);
echo var_dump($_SESSION['group']);
But they prints null value.
Thank You
I would suggest that you should use the following code instead of modifying the ini file:
this should be set before calling the session_start.
As you can see i didn't use ".localhost" use your host name instead.
If your going to test this code better to test this using Firefox because cookies with domains
will not work in chrome if the server is not located on the internet
for an instance the host name of my computer is "example-pc"
session_set_cookie_params (0, "/", ".example-pc", false, true);
then call the session start
session_start();

Phonebook in php without using MySQL

I just wanted to create a simple phonebook using php...I used the following code....but one entry overwrites another plz help...I want to do this without using MySQL
<?php
session_start();
if(isset($_SESSION['views']))
{
$_SESSION['views']=$_SESSION['views']+1;
}
else
{
$_SESSION['views']=1;
}
?>
<!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>Phonebook</title>
</head>
<body>
<form action="index1.php" method="post" style="border:thin">
Name: <input type="text" name="varname" style="border:dotted" />
<br/>
Roll Number:<input type="text" name="varroll" style="border:dotted" />
<br />
Phone Number: <input type="text" name="varno" style="border:dotted" />
<br/>
<input type="submit" name="submit" value="Register" /><br/>
</form>
<?php
$test1[$_SESSION['views']]=$_POST['varname'];
$test2[$_SESSION['views']]=$_POST['varroll'];
$test3[$_SESSION['views']]=$_POST['varno'];
for($j=1;$j<=$_SESSION['views'];$j++)
{
echo $test1[$j]." ".$test2[$j]." ".$test3[$j];}
echo "<br/>";
echo "No. of page views=".$_SESSION['views'];
?>
</body>
</html>
You could write it to a text file with | seperating each value or you could use an ini or xml file
You cannot just use $_SESSION, as it will be emptied when the user closes the browser.
Best to do something like this (sample untested code)
//loading
$data = unserialize( file_get_contents( 'mydata.txt' ) );
//editing
$entry = array();
$entry['roll']=$_POST['varroll'];
$entry['name']=$_POST['varname'];
$data[] = $entry;
//saving
file_put_contents( 'mydata.txt', serialize($data) );

How can save (multiple) entries from a html form to an array in php?

I have an HTML form with three fields, first name surname and mid. name and I want to save this information in a php page. The problem is I don't know how to save multiple entries in array without a connection of database.
HTML form
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Εργασία 11 - Βασίλης Τρίγκας</title>
</head>
<body>
<h4>Καταχώριση βιβλίου</h4>
<form action="form_action.php" method="get">
Συγγραφέας <br/><input type="text" name="fnwriter" /><br/>
Τίτλος <br/><input type="text" name="title" /><br/>
Εκδότης<br/> <input type="text" name="editor" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP File
<?php
function display_welcome(){
print("Σωστή εισαγωγή, ");
print($_GET['fnwriter','title','editor'])
}
function display_empty_form(){
print <<<_HTML_
<FORM method="post" action="$SERVER['PHP_SELF']">
<BR/>
<INPUT type="submit" value="SUBMIT NAME">
</FORM>
_HTML_;
}
if ($_GET['fnwriter','title']) {
display_welcome();
}
else {
display_empty_form();
}
$Book=array("fnwriter","title","editor");
$fwriter=$_GET['$fnwriter']
$title=$_GET['$title']
$editor=$_GET['$editor']
echo $fnwriter;
echo $title;
echo $editor;
for ($i = 0; $i < count($fnwriter,$title,$editor); ++$i) {
echo "Book $i= $fnwriter[$i],$title[$i],$editor[$i]<br />";
}
//echo $Book[$_GET];
//print_r($);
?>
I don't really get your question, is that about saving data to a file, or about processing $_POST data?
If it's the first one, you have a plenty of possibilities, from simple CSV (or any other separator) to XML ;)

Categories