Page 1 of 1
session variables
Posted: Tue Jun 15, 2010 3:35 pm
by nick2price
I am trying to work with session variables. I have a html page with a form. On submit, it calls up a php page. At the bottom of this php page, in the body section, I have
Code: Select all
<?php
$name = $_POST['sender'];
if($sender=="") {
echo "error";
} else {
echo "ok";
session_start();
session_register ("sender");
$_SESSION ["sender"] = $name;
}
?>
sender is the variable name of the input from the html form. This php page has another form, where the user can enter their email address. On submit, this calls up another form to send the email. This looks like
Code: Select all
<?php
session_start();
if(isset($_POST['Submit'])) {
$to = $_POST['receiver'];
$subject = "$name_field Has sent you an E-Card";
$name_field = $_POST['sender'];
$body = "Variable is " .$_SESSION['sender'];
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "ERROR!";
}
?>
So i think i am doing things correctly. The email gets sent, but it only displays 'Variable is '. For some reason, I am not getting my session variable there. Is there anything i am doing wrong?
cheers
Re: session variables
Posted: Tue Jun 15, 2010 3:55 pm
by andyhoneycutt
Could you post the entirety of these two php pages?
Re: session variables
Posted: Tue Jun 15, 2010 4:30 pm
by nick2price
Sure
First html page
Code: Select all
<!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>New Baby e-card demo</title>
<link rel= "stylesheet" type="text/css" href="../demoPages.css" />
<script src="../../Scripts/swfobject_modified.js" type="text/javascript"></script>
<script type="text/javascript" src="swfobject/swfobject.js"></script>
<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<style type="text/css">
<!--
a {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #FFF;
}
a:link {
text-decoration: underline;
}
a:visited {
text-decoration: underline;
color: #FFF;
}
a:hover {
text-decoration: none;
color: #00F;
}
a:active {
text-decoration: underline;
color: #F00;
}
-->
</style></head>
<body onload="MM_preloadImages('../webButtons/homeRollover.jpg','../webButtons/aboutUsRollover.jpg','../webButtons/contactUsRollover.jpg')">
<table class="demoPages">
<tr>
<td class="banner" colspan="4"><object id="myId" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="1150" height="170">
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" width="1150" height="170">
<param name="SRC" value="banner.swf" />
<!--<![endif]-->
<p></p>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
</td>
</tr>
<tr>
<td class="buttons" colspan="4"><a href="../../index.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('home','','../webButtons/homeRollover.jpg',1)"><img src="../webButtons/home.jpg" alt="Home" name="home" width="115" height="24" border="0" id="home" /></a><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('About Us','','../webButtons/aboutUsRollover.jpg',1)"><img src="../webButtons/about.jpg" alt="About Us" name="About Us" width="115" height="24" border="0" id="About Us" /></a><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('contact us','','../webButtons/contactUsRollover.jpg',1)"><img src="../webButtons/contactUs.jpg" alt="contact us" name="contact us" width="115" height="24" border="0" id="contact us" /></a></td>
</tr>
<tr>
<td colspan="2" class="title" ><p class="demo" >Anniversary E-card - Demo</p></td>
<td class="form" rowspan="2"><p> </p>
<p class="demo1">Send this e-card to your special person</p>
<form method="POST" onsubmit="return validateFormOnSubmit(this)" action="anniversaryPreview.php">
<p class="demo5">Senders Name<a class="textFields2">
<input type="text" name="sender" value="e.g. Love from Nick" size="30" maxlength="35" /></a></p>
<p class="btn2" >
<input type="Submit" value="Preview"></p>
</form>
</td>
<td class="right" rowspan="3"></td>
</tr>
<tr >
<td class="leftSide"></td>
<td class="movie" ><object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="480" height="360">
<param name="movie" value="anniversarySWF/anniversary2.swf" />
<param name="quality" value="high" />
<param name="wmode" value="opaque" />
<param name="swfversion" value="6.0.65.0" />
<!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you don’t want users to see the prompt. -->
<param name="expressinstall" value="Scripts/expressInstall.swf" />
<!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="anniversarySWF/anniversary2.swf" width="480" height="360">
<!--<![endif]-->
<param name="quality" value="high" />
<param name="wmode" value="opaque" />
<param name="swfversion" value="6.0.65.0" />
<param name="expressinstall" value="Scripts/expressInstall.swf" />
<!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. -->
<div>
<h4>Content on this page requires a newer version of Adobe Flash Player.</h4>
<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" width="112" height="33" /></a></p>
</div>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object></td>
</tr>
<tr>
<td class="linksContainer" colspan="3">
<br /><p ><a class="links" href="anniversary.html" >Anniversary</a> <a class="links" href="../HappyBirthday/birthday.html">Birthday</a><a class="links">Congrats</a><a href="../love/love.html" class="links">Love</a><a href="friends.html" class="links">Friends</a><a href="newBaby.html" class="links">New Baby</a></p></td>
</tr>
<tr>
<td class="bottomRow" colspan="4"></td>
</tr>
</table>
<script type="text/javascript">
<!--
swfobject.registerObject("myId", "9.0.115", "swfobject/expressInstall.swf");
swfobject.registerObject("FlashID");
//-->
</script>
</body>
</html>
The on submit calls up this php page
Code: Select all
<!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>New Baby e-card demo</title>
<link rel= "stylesheet" type="text/css" href="../demoPages.css" />
<script src="../../Scripts/swfobject_modified.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<style type="text/css">
<!--
a {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #FFF;
}
a:link {
text-decoration: underline;
}
a:visited {
text-decoration: underline;
color: #FFF;
}
a:hover {
text-decoration: none;
color: #00F;
}
a:active {
text-decoration: underline;
color: #F00;
}
-->
</style></head>
<body onload="MM_preloadImages('../webButtons/homeRollover.jpg','../webButtons/aboutUsRollover.jpg','../webButtons/contactUsRollover.jpg')">
<table class="demoPages">
<tr>
<td class="banner" colspan="4"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="1150" height="170">
<param name="movie" value="../swf/banner.swf" />
<param name="quality" value="high" />
<param name="wmode" value="opaque" />
<param name="swfversion" value="6.0.65.0" />
<!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you don’t want users to see the prompt. -->
<param name="expressinstall" value="../../Scripts/expressInstall.swf" />
<!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="../swf/banner.swf" width="1150" height="170">
<!--<![endif]-->
<param name="quality" value="high" />
<param name="wmode" value="opaque" />
<param name="swfversion" value="6.0.65.0" />
<param name="expressinstall" value="../../Scripts/expressInstall.swf" />
<!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. -->
<div>
<h4>Content on this page requires a newer version of Adobe Flash Player.</h4>
<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" width="112" height="33" /></a></p>
</div>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object></td>
</tr>
<tr>
<td class="buttons" colspan="4"><a href="../../index.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('home','','../webButtons/homeRollover.jpg',1)"><img src="../webButtons/home.jpg" alt="Home" name="home" width="115" height="24" border="0" id="home" /></a><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('About Us','','../webButtons/aboutUsRollover.jpg',1)"><img src="../webButtons/about.jpg" alt="About Us" name="About Us" width="115" height="24" border="0" id="About Us" /></a><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('contact us','','../webButtons/contactUsRollover.jpg',1)"><img src="../webButtons/contactUs.jpg" alt="contact us" name="contact us" width="115" height="24" border="0" id="contact us" /></a></td>
</tr>
<?php
$name = $_POST['sender'];
if($sender=="") {
echo "error";
} else {
echo "ok";
session_start();
session_register ("sender");
$_SESSION ["sender"] = $name;
}
?>
<tr>
<td colspan="2" class="title" ><p class="demo" >Anniversary - Personalised E-card</p></td>
<td class="form" rowspan="2"><p class="demo3">Send a link to this e-card for free</p>
<form method="POST" onsubmit="return validateFormOnSubmit1(this)" action="sendEmail.php">
<p class="demo2">Receivers E-mail<a class="textFields">
<input type="text" name="receiver" size="30" maxlength="35" /></a></p>
<p class="demo2">Senders E-mail<a class="textFields2">
<input type="text" name="sender" size="30" maxlength="35" /></a></p>
<p class="btn" >
<input name="Submit" type="submit" value="Send"/></p>
</form>
<p class="demo3">Or purchase your own hard copy for just £3.00</p>
<p class="demo4">Hard copies are completely add free, and will be sent to your e-mail address as an SWF file. You gain full ownership over this file, and you can personally e-mail it to the recipient.</p>
</td>
<td class="right" rowspan="3"></td>
</tr>
<tr >
<td class="leftSide"></td>
<td class="movie" >
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="480" height="360" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="anniversarySWF/anniversaryComplete.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#FFFFFF" />
<param name="flashvars" value="myVar1=<?php echo $_POST['sender'] ?>" />
<param name="LOOP" value="false" />
<embed src="anniversarySWF/anniversaryComplete.swf" width="480" height="360" loop="false" align="middle" FlashVars="myVar1=<?php echo $_POST['sender'] ?>" quality="high" bgcolor="#FFFFFF" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</td>
</tr>
<tr>
<td class="linksContainer" colspan="3">
<br /><p ><a class="links" href="anniversary.html" >Anniversary</a> <a class="links" href="../HappyBirthday/birthday.html">Birthday</a><a class="links">Congrats</a><a href="../love/love.html" class="links">Love</a><a href="../friends/friends.html" class="links">Friends</a><a href="newBaby.html" class="links">New Baby</a></p></td>
</tr>
<tr>
<td class="bottomRow" colspan="4"></td>
</tr>
</table>
</body>
</html>
It is php because I use flashvars which needs php. I also done the sessions in there. The on submit calls up my email code which in its entirity, is listed in the op.
Cheers for the help
Re: session variables
Posted: Tue Jun 15, 2010 4:37 pm
by andyhoneycutt
session_register() has been deprecated. What happens when you remove it from your code, and just rely on the preferred method of assignment: $_SESSION['key_name'] = $variable_input
-Andy
Re: session variables
Posted: Tue Jun 15, 2010 4:45 pm
by nick2price
I actually had some variable misnaming in my first php file. It works now, but if that method has been depreciated, should i remove it?
Re: session variables
Posted: Tue Jun 15, 2010 4:57 pm
by andyhoneycutt
In this case absolutely. You're performing the assignment to the session immediately below the register function call. It's unnecessary as well as being deprecated

Re: session variables
Posted: Tue Jun 15, 2010 5:08 pm
by nick2price
kool. What I am aiming to do is this. The php that sends the email will be sent to someone who has not been on my page before. This email will be a link to a page. This page will display a flash movie which will display the session variable. How can I send the session variable to the page when they click on the link?
cheers
Re: session variables
Posted: Tue Jun 15, 2010 5:11 pm
by andyhoneycutt
I would just pass it in the link that they click on. base64_encode it and send it as part of the $_GET string. Decode it on the other side.
Re: session variables
Posted: Tue Jun 15, 2010 5:53 pm
by nick2price
Do you know of any examples of stuff like that? So in my case, would it just be something like
www.myUrl.com/pagePreview.html?.$_SESSION['sender']
Re: session variables
Posted: Wed Jun 16, 2010 9:15 am
by andyhoneycutt
To send the values (using base64_encode):
Code: Select all
$url = "http://www.myurl.com/pagepreview.html";
$sender = base64_encode($_SESSION['sender']);
$full_url = "$url?sender=$sender";
Now, to retrieve from the url:
Code: Select all
$sender = base64_decode($_GET['sender']);