Log in

View Full Version : How to use Session in PHP like ASP



paramjeetgemini
03-25-2009, 07:14 AM
Hello,

I am trying to make a form that will, when submitted, will call "send.php" to process the form fields and then redirect to a "thanks.php" page.

Issue is, I want the "thanks.php" page to show the values of that form field submitted like Name, Product chosen, order No., Amount etc.

I have done similar kind of thing in ASP using sessions but unable to do it in PHP.

I am not using any database here. Just a simple "form mail" which sends the form contents to email & shows a "thanks.php" page for the confirmation or you may say customized Thanks to the user.

The PHP Code i.e. "send.php" I am using is as under:



<?php
$nameofproduct = $_REQUEST['nameofproduct'] ;
$priceusd = $_REQUEST['priceusd'] ;

$nametitle = $_REQUEST['nametitle'] ;
$name = $_REQUEST['name'] ;
$address = $_REQUEST['address'] ;
$city = $_REQUEST['city'] ;
$district = $_REQUEST['district'] ;
$state = $_REQUEST['state'] ;
$country = $_REQUEST['country'] ;
$pincode = $_REQUEST['pincode'] ;

$telephone = $_REQUEST['telephone'] ;
$email = $_REQUEST['email'] ;
$query = $_REQUEST['query'] ;

$uorder = date("dmY-His") ;

$userdetails = $_SERVER['REMOTE_ADDR'];
$ourid = "Company Name <info@company.com>";

$message = "<h4><font color=blue>$nametitle $name has placed an order. Details are as under</font></h4>
<br><br><b><u>CONTACT/SHIPPING DETAILS:</u></b>
<br><b>Name: </b> $title $name
<br><br><b>Address: </b> $address
<br><br><b>Telephone: </b> $telephone
<br><br><b>E-mail ID: </b> $email
<br><br><b>Any other Information/Question: </b> $query
<br><br><b>IP Address of Customer is: </b> $userdetails
<br><br><br><b><u>ORDER DETAILS</u></b>
<br><b>Order No.: </b> $uorder
<br><b>Name of Product: </b> PRODUCT NAME
<br><br><b>Total Amount (in US Dollars): </b> $priceusd ";



$repmessage = "<table border=0 width=600>
<tr><td><br>
<b>Dear $title $name</b>
<br><br>Thanks for your order. Your order details:
<br><br>Order No.: $uorder
<br><br>Name of Product: PRODUCT NAME
<br><br>Rate Per Unit in US Dollars: $priceusd
<br><br>Amount in USD: $priceusd
<br><br><br>
<br><br>Admin Team
<br><br>Company Name
<br>http://www.company.com</tr></td></table>";

$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers.= "From: $email\r\n";

$repheaders = "MIME-Version: 1.0\r\n";
$repheaders.= "Content-type: text/html; charset=iso-8859-1\r\n";
$repheaders.= "From: $ourid\r\n";


mail( "info@company.com", "Order No. $uorder received from website",
$message, $headers );

mail( "To: $email", "Thanks for your shopping at company.com",
$repmessage, $repheaders );


header( "Location: http://www.company.com/thanks.php" );
?>


The above code works just fine to send the form contents to the email. But I am not able to display the form contents in thanks.php page.

Actually I am neither an ASP programmar nor a PHP programmar as well. But knows how to do this in ASP. PHP is very new for me, thats why unable to do this.

Any help will be highly appreciated.

biggjoe
03-25-2009, 08:32 PM
send.php can do the processing and emailing and still display all the items on the page.

From your codes, you did not 'echo' the submitted values all you did is just put that there.

They cant just show themselves, you have to print them to the screen. There are many articles to read on php. I a, relatively new in php too but its much easier to deal with than ASP which i also started with.

paramjeetgemini
03-26-2009, 07:12 AM
Hello,

Thanks a lot for your replies.

Actually my form works like this:

1. HTML Form: order.htm
When submitted, it calls send.php to process it.

2. PHP Form processor Code: send.php
It processes the form in order.htm and sends the form details to the email and then displays confirmation page i.e. thanks.php

3. Confirmation Page: thanks.php
In this I am looking to fetch details of form submitted. The fileds would be Name etc.

Below is the code of all the above files one by one

CODE FOR ORDER.HTM


<html>

<head>
<title>
Order Form
</title>

</head>

<body>

<form method="post" action="send.php" name="form1">

<u><b>PRODUCT DETAILS: </b></u>
</br>
</br>

<input type="text" name="nameofproduct" size="45" readonly value="PRODUCT NAME">
</br>
</br>


<b>Price in USD: </b>
<input type="text" name="priceusd" size="5" value="1" readonly>
</br>
</br>
</br>
</br>


<b><u>CONTACT DETAILS</b></u>
</br>
</br>
<b>Title: </b>
<select name="nametitle">
<option selected value="Mr.">Mr.</option>
<option value="Ms.">Ms.</option>
</select>
</br>
</br>

<b>Full Name: </b>
<input type="text" name="name" size="35">
</br>
</br>

<b>Address: </b>
<textarea rows="5" name="address" size="30" cols="35"></textarea>
</br>
</br>
</br>
</br>

<b><u>Details about City, District, State, Country, PIN/ZIP Code</b></u>
</br>
</br>

<b>City: </b>
<input name="city" size="23">
</br>
</br>

<b>District: </b>
<input name="district" size="23">
</br>
</br>

<b>State: </b>
<input name="state" size="23">
</br>
</br>

<b>Country: </b>
<input name="country" size="23">
</br>
</br>

<b>PIN/ZIP Code: </b>
<input name="pincode" size="23">
</br>
</br>

<b>Telephone: </b>
<input name="telephone" size="35">
</br>
</br>

<b>E-mail: </b>
<input name="email" size="35">
</br>
</br>

<b>Any other information/query: </b>
<textarea rows="5" name="query" size="30" cols="35"></textarea>
</br>
</br>

<input type="submit" value="Submit">

</form>

</body>

</html>


CODE FOR THANKS.PHP



<html>

<head>
<title>
Thanks for your order
</title>

</head>

<body>

Dear <?php echo $_Request['nametitle'] ?> <?php echo $_Request['name'] ?>
</br></br>
THANKS FOR YOUR ORDER.

WE WILL GET BACK TO YOU SOON

</body>

</html>


Actually I am not aware of the syntax to use in send.php. But I know that in ASP we write e.g. session("name")= name to create a session variable and store the value of form field in it and then we can fetch it using <%=session("productname")%>.

Hope someone can guide me to a very basic thing in PHP.

Ahmed Saleh
03-26-2009, 07:54 PM
hello

if you want to use SESSIONS on PHP language it mean that you need to do this step at first :

write this function at the first line on all php files includes sessions like :



<?php session_start() ;

// ON recieve file .

$_SESSION[name] = $value ;
$_SESSION[name2] = $value2 ;
$_SESSION[name3] = $value3 ;

//------- OR --------//

$_SESSION[username] = $_POST[username] ;
$_SESSION[email] = $_POST[email] ;

?>


NOW YOU HAVE MANY SESSION VARIABLES , AND YOU CAN USE THEM ON ANY FILE INCLUDE THIS FUNCTION ( session_start(); ) ON FIRST LINE ON YOUR SCRIPT FILES .

paramjeetgemini
03-26-2009, 08:26 PM
Dear Ahmed Saleh,

Thanks for the reply.

I have tried using session but its not helping and the Thanks Page displays no values (of form submitted) at all.

I am looking to display the "Values" of Form Submitted in the Thanks Page (the code for these are Order.htm which contains form, send.php which processes this form to send the form results to email and then redirects to Thanks Page thanks.php as mentioned in my previous two posts.).

Can you plz let me know how to fetch Values of Form e.g. "Name" in the Thanks Page i.e. thanks.php ?

borris83
03-27-2009, 05:58 AM
Hi, Make sure that you have these things right:

The command php session_start() ; should appear before any other html code and it should appear on both the pages....


You also have to assign all the variables of array $_REQUEST to the array $_SESSION

for eg:

$_SESSION['nameofproduct'] = $_REQUEST['nameofproduct'] ;
$_SESSION['priceusd'] = $_REQUEST['priceusd'] ;


and in thanks.php, you have to echo the values of $_SESSION['nameofproduct'], $_SESSION['priceusd'] and so on

Remember, the function session_start() ; should appear before your very first html code on the page.. If there is even one html code before session_start(), the it might not work

Ahmed Saleh
03-27-2009, 12:14 PM
Dear Ahmed Saleh,

Thanks for the reply.

I have tried using session but its not helping and the Thanks Page displays no values (of form submitted) at all.

I am looking to display the "Values" of Form Submitted in the Thanks Page (the code for these are Order.htm which contains form, send.php which processes this form to send the form results to email and then redirects to Thanks Page thanks.php as mentioned in my previous two posts.).

Can you plz let me know how to fetch Values of Form e.g. "Name" in the Thanks Page i.e. thanks.php ?


Okay ,
in send.php you will send the email and then redirect the visitor to thanks.php
right ?

We can do another thing like :

we will show the tanks message on the send.php file

like :




<?php
$nameofproduct = $_REQUEST['nameofproduct'] ;
$priceusd = $_REQUEST['priceusd'] ;

$nametitle = $_REQUEST['nametitle'] ;
$name = $_REQUEST['name'] ;
$address = $_REQUEST['address'] ;
$city = $_REQUEST['city'] ;
$district = $_REQUEST['district'] ;
$state = $_REQUEST['state'] ;
$country = $_REQUEST['country'] ;
$pincode = $_REQUEST['pincode'] ;

$telephone = $_REQUEST['telephone'] ;
$email = $_REQUEST['email'] ;
$query = $_REQUEST['query'] ;

$uorder = date("dmY-His") ;

$userdetails = $_SERVER['REMOTE_ADDR'];
$ourid = "Company Name <info@company.com>";

$message = "<h4><font color=blue>$nametitle $name has placed an order. Details are as under</font></h4>
<br><br><b><u>CONTACT/SHIPPING DETAILS:</u></b>
<br><b>Name: </b> $title $name
<br><br><b>Address: </b> $address
<br><br><b>Telephone: </b> $telephone
<br><br><b>E-mail ID: </b> $email
<br><br><b>Any other Information/Question: </b> $query
<br><br><b>IP Address of Customer is: </b> $userdetails
<br><br><br><b><u>ORDER DETAILS</u></b>
<br><b>Order No.: </b> $uorder
<br><b>Name of Product: </b> PRODUCT NAME
<br><br><b>Total Amount (in US Dollars): </b> $priceusd ";



$repmessage = "<table border=0 width=600>
<tr><td><br>
<b>Dear $title $name</b>
<br><br>Thanks for your order. Your order details:
<br><br>Order No.: $uorder
<br><br>Name of Product: PRODUCT NAME
<br><br>Rate Per Unit in US Dollars: $priceusd
<br><br>Amount in USD: $priceusd
<br><br><br>
<br><br>Admin Team
<br><br>Company Name
<br>http://www.company.com</tr></td></table>";

$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers.= "From: $email\r\n";

$repheaders = "MIME-Version: 1.0\r\n";
$repheaders.= "Content-type: text/html; charset=iso-8859-1\r\n";
$repheaders.= "From: $ourid\r\n";

// i will edit from here

mail( "info@company.com", "Order No. $uorder received from website",
$message, $headers );

$final_send = mail( "To: $email", "Thanks for your shopping at company.com",
$repmessage, $repheaders );


if ($final_send)
{
echo "The Thanks Message here !"; // write all that you want here .
}

?>





Try it and let me know the result .

CrazyChop
03-28-2009, 06:48 AM
Just one thing

Are you running this script on your own computer with a PHP setup? If so, make sure you set the temporary path for session inside your php.ini.

paramjeetgemini
03-28-2009, 05:42 PM
Okay ,
in send.php you will send the email and then redirect the visitor to thanks.php
right ?

We can do another thing like :

we will show the tanks message on the send.php file

like :




<?php
$nameofproduct = $_REQUEST['nameofproduct'] ;
$priceusd = $_REQUEST['priceusd'] ;

$nametitle = $_REQUEST['nametitle'] ;
$name = $_REQUEST['name'] ;
$address = $_REQUEST['address'] ;
$city = $_REQUEST['city'] ;
$district = $_REQUEST['district'] ;
$state = $_REQUEST['state'] ;
$country = $_REQUEST['country'] ;
$pincode = $_REQUEST['pincode'] ;

$telephone = $_REQUEST['telephone'] ;
$email = $_REQUEST['email'] ;
$query = $_REQUEST['query'] ;

$uorder = date("dmY-His") ;

$userdetails = $_SERVER['REMOTE_ADDR'];
$ourid = "Company Name <info@company.com>";

$message = "<h4><font color=blue>$nametitle $name has placed an order. Details are as under</font></h4>
<br><br><b><u>CONTACT/SHIPPING DETAILS:</u></b>
<br><b>Name: </b> $title $name
<br><br><b>Address: </b> $address
<br><br><b>Telephone: </b> $telephone
<br><br><b>E-mail ID: </b> $email
<br><br><b>Any other Information/Question: </b> $query
<br><br><b>IP Address of Customer is: </b> $userdetails
<br><br><br><b><u>ORDER DETAILS</u></b>
<br><b>Order No.: </b> $uorder
<br><b>Name of Product: </b> PRODUCT NAME
<br><br><b>Total Amount (in US Dollars): </b> $priceusd ";



$repmessage = "<table border=0 width=600>
<tr><td><br>
<b>Dear $title $name</b>
<br><br>Thanks for your order. Your order details:
<br><br>Order No.: $uorder
<br><br>Name of Product: PRODUCT NAME
<br><br>Rate Per Unit in US Dollars: $priceusd
<br><br>Amount in USD: $priceusd
<br><br><br>
<br><br>Admin Team
<br><br>Company Name
<br>http://www.company.com</tr></td></table>";

$headers = "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers.= "From: $email\r\n";

$repheaders = "MIME-Version: 1.0\r\n";
$repheaders.= "Content-type: text/html; charset=iso-8859-1\r\n";
$repheaders.= "From: $ourid\r\n";

// i will edit from here

mail( "info@company.com", "Order No. $uorder received from website",
$message, $headers );

$final_send = mail( "To: $email", "Thanks for your shopping at company.com",
$repmessage, $repheaders );


if ($final_send)
{
echo "The Thanks Message here !"; // write all that you want here .
}

?>





Try it and let me know the result .
Thanks for the reply Ahmed Saleh. Actually ditto same thing I had already tried just 2 days ago before your reply. It works as well. But even this has some issue. When someone refreshes/Click Back Button of Browser in the Thanks.php Page (which is actually now working as send.php as well because the Thanks.php & Send.php are one file), the form gets submitted again and a new order code etc. gets generated.

Is it possible that if someone refreshes or click browser back button the form does not get submitted again ?

paramjeetgemini
03-28-2009, 05:44 PM
Just one thing

Are you running this script on your own computer with a PHP setup? If so, make sure you set the temporary path for session inside your php.ini.
Thanks for the reply Crazychop.

Actually I am testing directing onto web hosting server as I have no idea about setting up PHP in Local System :). So I dont think there is any case for editing php.ini.

paramjeetgemini
03-28-2009, 05:45 PM
Hi, Make sure that you have these things right:

The command php session_start() ; should appear before any other html code and it should appear on both the pages....


You also have to assign all the variables of array $_REQUEST to the array $_SESSION

for eg:

$_SESSION['nameofproduct'] = $_REQUEST['nameofproduct'] ;
$_SESSION['priceusd'] = $_REQUEST['priceusd'] ;


and in thanks.php, you have to echo the values of $_SESSION['nameofproduct'], $_SESSION['priceusd'] and so on

Remember, the function session_start() ; should appear before your very first html code on the page.. If there is even one html code before session_start(), the it might not work
Thanks for the reply borris83

I am testing this and would let you know if this works :).

Thanks to all of you for your priceless time.

Ahmed Saleh
03-29-2009, 03:31 PM
Thanks for the reply Ahmed Saleh. Actually ditto same thing I had already tried just 2 days ago before your reply. It works as well. But even this has some issue. When someone refreshes/Click Back Button of Browser in the Thanks.php Page (which is actually now working as send.php as well because the Thanks.php & Send.php are one file), the form gets submitted again and a new order code etc. gets generated.

Is it possible that if someone refreshes or click browser back button the form does not get submitted again ?


I know , kindely you can add this code at the end :



echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3; url=the form url here\">";


replace : the form url here
write : the form page name , like index.php .

and it will fine ...

Try and let me know the result .

paramjeetgemini
03-29-2009, 07:15 PM
I know , kindely you can add this code at the end :



echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3; url=the form url here\">";


replace : the form url here
write : the form page name , like index.php .

and it will fine ...

Try and let me know the result .
Thanks Ahmed Saleh.

But Where should this line be added in the code?



echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3; url=the form url here\">";


I mean in send.php file ? If yes, then where should this line be written ?

At top where variables are declared ? or at the end ?

Waiting for the reply...

paramjeetgemini
03-29-2009, 07:34 PM
Dear Ahmed Saleh,

Actually I tried it somewhere in my send.php (which now also working as thanks page as well). But issue is the page gets refreshed back to the Form by using the code:

echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3; url=order.htm\">";

I am looking for something if someone clicks on BACK BUTTON in BROWSER, the message should be that the page has expired or something similar. And this way, the form will not get submitted again or atleast the send.php will not get executed again.

By refereshing send.php file (i.e. now it is a thanks page), the code gets executed on the server once again.

Can we add any line at the bottom of send.php so that this page, even if refereshed, does nothing ?

paramjeetgemini
03-29-2009, 08:01 PM
Hi, Make sure that you have these things right:

The command php session_start() ; should appear before any other html code and it should appear on both the pages....


You also have to assign all the variables of array $_REQUEST to the array $_SESSION

for eg:

$_SESSION['nameofproduct'] = $_REQUEST['nameofproduct'] ;
$_SESSION['priceusd'] = $_REQUEST['priceusd'] ;


and in thanks.php, you have to echo the values of $_SESSION['nameofproduct'], $_SESSION['priceusd'] and so on

Remember, the function session_start() ; should appear before your very first html code on the page.. If there is even one html code before session_start(), the it might not work
No dear, this still does not work. I mean No Error but not able to fetch values in thanks.php

Ahmed Saleh
03-30-2009, 02:00 PM
if you want show : page expired message
put this at the first on send.php .





header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");



Now your script is working so good and like you want to be .:)

paramjeetgemini
03-30-2009, 03:42 PM
Dear Ahmed Saleh,

Thanks once again for your help. But even after placing this code at top of my send.php, I am able to refresh it (i.e. thanks confirmation page). The form gets submits again, the order gets generated again :(

Hope there is a way so that even if someone refreshes the thanks confirmation page, it does not submits the form again.

Anyway, thanks a lots for your time.