Setting session variable using text box onChange event.
Moderator: General Moderators
Setting session variable using text box onChange event.
I need to set a session variable on the fly. I was using cookies, but I need way too many (much more than the 20 allowed):
<script language="JavaScript">
function setCookie (name, value)
{
document.cookie = name + '=' + escape(value)
}
</script>
<input type="text" name="MiddleName" onChange=
"setCookie('MiddleName', MiddleName.value)"
value="<?php echo "{$_COOKIE['MiddleName']}"; ?>" size="25">
Is there any way to do this without cookies?
<script language="JavaScript">
function setCookie (name, value)
{
document.cookie = name + '=' + escape(value)
}
</script>
<input type="text" name="MiddleName" onChange=
"setCookie('MiddleName', MiddleName.value)"
value="<?php echo "{$_COOKIE['MiddleName']}"; ?>" size="25">
Is there any way to do this without cookies?
Okay, got that far, but it poses another problem. Now I need to dynamically name a session variable. I'm fairly new to sessions, so please bear with me. I tried something like this (which doesn't work):
What I was trying to do was make $_SESSION['SomeName']="SomeValue" dynamically. Can this be done?
Code: Select all
<?php
session_start();
$SessionName = "SomeName";
$_SESSION[$SessionName] = "SomeValue";
?>Try this:hmartyb wrote:I'll play with it a bit more tomorrow. $_SESSION['SomeName'] may or may not have a value, as I'm tying this bit of code to my header request. I'll post the a better picture of what I'm trying to accomplish tomorrow, thanks for your help so far!!
Marty
Code: Select all
session_start();
$name='foo';
// You have to give it a value or it 'won't work' (won't output a value)
$_SESSION['foo']='bar';
echo $_SESSION[$name];I was afraid of that. I was trying to get around having to do this:
By doing this:
As I will be setting about 80 session variables.
Code: Select all
<?php
session_start();
$name = stripslashes($_GET['name']);
$value = stripslashes($_GET['value']);
switch ($name)
{
case "FirstName":
$_SESSION['FirstName'] = $value;
break;
case "LastName":
$_SESSION['LastName'] = $value;
break;
}
echo $name ." = ".$_SESSION[$name];
?>Code: Select all
<?php
session_start();
$name = $_GET['name'];
$value = $_GET['value'];
$_SESSION['$name']=$value;
echo $_SESSION[$name];
?>NEVER do:
This poses the same risks as leaving register globals on, what happens when someone sets $name to 'admin' and $value to 1? Although, yes this code would do what you intended it would leave a huge security hole.
Try something like this:
This should populate your session data as long as you specify all the allowed values.
Code: Select all
<?php
session_start();
$name = $_GET['name'];
$value = $_GET['value'];
$_SESSION['$name']=$value;
echo $_SESSION[$name];
?>Try something like this:
Code: Select all
$array=array("list", "of", "acceptable", "session", "names");
$count=count($array);
for ($x=0; $x<=$count; $x++) {
$_SESSION[$array[$x]]=htmlentities($_POST[$array[$x]]);
}
print_r($_SESSION);Yeah, I was so intent on solving my coding, it didn't occur to me! Thanks for pointing it out! Also thanks for steering me in the right direction on setting my session variables. I think you've answered all of my questions.This poses the same risks as leaving register globals on, what happens when someone sets $name to 'admin' and $value to 1?
Marty