Go Back   Zaheema Social Network > Community > Help!

Closed Thread
 
Thread Tools Rating: Thread Rating: 15 votes, 5.00 average. Display Modes

Default Understanding PHP Session
  #1  
Old 09-24-2007, 04:50 AM
junaid junaid is offline
Administrator
 
Join Date: May 2007
Location: Pakistan
Posts: 133
Send a message via MSN to junaid Send a message via Yahoo to junaid
Session Overview
When User visit your website, a web session has been created. PHP Session Management Allow you to track/manipulate User Data during this web session.
Points To Remember
  • Session can be destroyed by closing the Browser, or any other Custom Mechanism like Logout function in most of scripts.
  • Session create Unique Identification number (UID) for each visitor to store variables.
  • Session Data can be stored automatically by PHP or user defined data storage like Mysql.
  • PHP Store $PHPSESSID in user cookies, to track User Identification Number.

Starting a Session
You can start PHP Session by following statement, at the start of PHP Page.
PHP Code:
session_start(); 
This function will register the user's session on server, allow you to start saving user information and assign a UID (unique identification number) for that user's web session.

Storing Session Variables
You can store Session variables in Associative Array $_SESSION,
Let see it By Example
PHP Code:
//Starting the Session
session_start();
$visit=$_SESSION['views'];

echo 
"You Have Visited this Page For ".$visit;

//Increment the Session Variable
$_SESSION['views']=$_SESSION['views']+1
Explanation
1. Starting the session
2. Reteriving the Session Variable "views", and assign the value to local variable
3. Print the Statement to Browser
4. Modify the "views" session variable and assign a new value.

So Every time user will visit the page (in current session) it increments it page view, and thus tell him/her that how many times he visited the page.

Destroy Session
Although Session is created for Temporary Period, and destroy itself when user end its web session. But in few cases you needed to destroy session, "Logout" is good example of destroying session. Or Timeout.
You Can Destroy Session just be simple one function call
PHP Code:
session_destroy(); 
Creating a Multi page Form to Email

php_mail1.php

PHP Code:
session_start();

//Check if user Submitted the Form
if($_POST['submit'])
{

// This form gather two parameters mail_to and mail_from

echo "<form method=post action=".$_SERVER['PHP_SELF'].">";
echo 
"<table cellpadding=0 cellspacing=0 width=100%>";
echo 
"<tr>";
echo 
"<td width=50%>Email Address To</td>";
echo 
"<td width=50%><input type=text name=mail_to></td>";
echo 
"</tr>";

echo 
"<tr>";
echo 
"<td width=50%>Email Address From</td>";
echo 
"<td width=50%><input type=text name=mail_from></td>";
echo 
"</tr>";

echo 
"<tr>";
echo 
"<td width=100% colspan=2><input type=submit name=submit value=submit></td>";
echo 
"</tr>";

echo 
"</table>";
echo 
"</form>";

}else{

//Assign the Values to Session Variable
$_SESSION['mail_to']=$_POST['mail_to'];
$_SESSION['mail_from']=$_POST['mail_from'];

//Redirect to second page
HEADER("Location: php_mail2.php");


php_mail2.php

We will gather Subject and Message Form Parameters.

PHP Code:
session_start();

//Check if user Submitted the Form
if($_POST['submit'])
{

// This form gather two parameters mail_to and mail_from

echo "<form method=post action=".$_SERVER['PHP_SELF'].">";
echo 
"<table cellpadding=0 cellspacing=0 width=100%>";
echo 
"<tr>";
echo 
"<td width=50%>Subject</td>";
echo 
"<td width=50%><input type=text name=mail_subject></td>";
echo 
"</tr>";

echo 
"<tr>";
echo 
"<td width=50%>Small Message</td>";
echo 
"<td width=50%><input type=text name=mail_message></td>";
echo 
"</tr>";

echo 
"<tr>";
echo 
"<td width=100% colspan=2><input type=submit name=submit value=submit></td>";
echo 
"</tr>";

echo 
"</table>";
echo 
"</form>";

}else{

//Assign the Values to Session Variable
$_SESSION['mail_subject']=$_POST['mail_subject'];
$_SESSION['mail_message']=$_POST['mail_message'];

//Redirect to second page
HEADER("Location: php_mail3.php");


php_mail3.php
PHP Code:
session_start();

//Check User Come from Previous Pages, just check that Session Variables
//Exist using isset() function

if(isset($_SESSION['mail_to']) && isset($_SESSION['mail_from']) && isset($_SESSION['mail_subject']) && isset($_SESSION['mail_message']))
{

//Send Mail using PHP Mail() function

mail($_SESSION['mail_to'],$_SESSION['mail_subject'],$_SESSION['mail_message'],$_SESSION['mail_from']);

echo 
"You have successfully sent email to ".$_SESSION['mail_to']." from ".$_SESSION['mail_from'];

//destroy the session, so user cannot spam, by refreshing the page
destroy_session();

}else{

echo 
"Invalid Parameters";


__________________



Default Not working...
  #2  
Old 10-03-2008, 03:03 PM
cbf
 
Posts: n/a
I'm getting a "Invalid Parameters" from php_email3.php. Don't you have to pass the SID in the query string to the downstream pages?

BTW, I had to change the line:

if($_POST['submit'])

to:

if(!$_POST['submit'])

in both php_email1.php and php_email2.php.

Thanks.

\cbf

Default
  #3  
Old 10-04-2008, 01:29 AM
junaid junaid is offline
Administrator
 
Join Date: May 2007
Location: Pakistan
Posts: 133
Send a message via MSN to junaid Send a message via Yahoo to junaid
Hi CBF
Thanks for mentioning it, It was my mistake Kindly Change in php_mail2.php

Notice submit box name is submit2

PHP Code:
echo "<td width=100% colspan=2><input type=submit name=submit2 value=submit></td>"
[h3]Why it was not working?[/h3]
As you can see in the php_mail1.php and php_mail2.php, the Submit name was unique "submit" therefore script php_mail2.php halt in the same block, as I have changed the submit name to "submit2", so this time when you submit the form there would be no such $_POST['submit'], and php switch to second block and redirect you third page, I hope you understand now.
__________________


Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 09:48 PM.