PHP - Help With Defining The Session Variable
I am trying to define a session variable where I can save it and use it as the user surfs the site.
I need the variable saved as $amano so I can use it in my select from/where statement and to echo within a table. This is a test trying to capture and define the variable and works, but I can't get the variable $amano into the session. If I am then I don't know how to display it. <?php> session_start(); $id = $_POST['amano']; $_SESSION['amano'] = '$amano'; echo "Pageviews = ". $_SESSION['amano']; // My effort to see what is happening. echo "<br />"; echo "AMA # = ". $_POST['amano']; // I have it just like I want it here. echo "<br />"; echo "Sessions AMA # = ".$_SESSION['amano']; ?> Similar TutorialsHi, I my first problem is hashing passwords to md5. My second problem is defining session on value from db. There is my code but not working. Code: [Select] mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $username=$_POST['username']; $password=$_POST['password']; $hash = md5($password); $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $sql="SELECT * FROM $tbl_name WHERE where username = '$username' and password = '$hash'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1){ $sql2="SELECT access FROM $tbl_name WHERE username='$username' and password='$password'"; $access=mysql_query("$sql2"); session_register("username"); session_register("password"); session_register("access"); $_SESSION["access"]=$access; header("location:success.php"); } else { echo "Invalid Username or Password";Thanks for any answers. // does the product exist ? $sql = "SELECT pd_id, pd_qty FROM tbl_product WHERE pd_id = $productId"; $result = dbQuery($sql); if (dbNumRows($result) != 1) { // the product doesn't exist header('Location: cart.php'); } else { // how many of this product we // have in stock $row = dbFetchAssoc($result); $currentStock = $row['pd_qty']; if ($currentStock == 0) { // we no longer have this product in stock // show the error message setError('The product you requested is no longer in stock'); header('Location: cart.php'); exit; } } // current session id $sid = session_id(); session_register("size"); $size = $_SESSION['size']; // check if the product is already // in cart table for this session $sql = "SELECT pd_id FROM tbl_cart WHERE pd_id = $productId AND ct_session_id = '$sid'"; $result = dbQuery($sql); if (dbNumRows($result) == 0) { // put the product in cart table $sql = "INSERT INTO tbl_cart (pd_id, ct_qty, size, ct_session_id, ct_date) " . "VALUES ($productId, 1, '$size', '$sid', NOW())"; $result = dbQuery($sql); } else { // update product quantity in cart table $sql = "UPDATE tbl_cart SET ct_qty = ct_qty + 1 WHERE ct_session_id = '$sid' AND pd_id = $productId"; $result = dbQuery($sql); } This is just a piece of my cartfunctions.php (which is an include() once you add an item to the cart). I've been trouble shooting this all day and finally got it to stop giving me error messages. Im trying to allow the user to pick a size between 4 options, after that it is added to the cart and I can easily find out what size shirt the customer wants before I ship it. but now it wont update the $size variable to the DB. Am I defining this wrong or maybe my whole approach is messed up? If you would like to see the setup you can go to rbcrime.com/newshop/newshop/ . I know that I have posted a similar question to this but I am still having some confusion that I hoped I could clear up. Thank you all for being patient with and helping me clean up this mess.
Below is the code that is spitting out the bottom error messages. So, I understand that the way to define a variable which uses += and not send it into a loop, generating a notice is this..
Using the top example
$meter_multiplier=0;
while ($row5 = sqlsrv_fetch_array($query5)){ meter_multiplier += $row5['meter_multiplier']; } versus... $meter_multiplier += $row5['meter_multiplier']; how can I implement this type of statment below. Should the php be called another name and reference sql that way, so the names are different? example $meter_multiplier=0; while ($row5 = sqlsrv_fetch_array($query5)){ meter_multiplier += $row5['meter_mult']; <?php $sql5 = "select meter_id, subaccount_number, service_name, service_address, service_address2, service_city, service_st, service_zip, service_contact, basic_charge, energy_charge, base_rate, intermediate_rate, peak_rate, meter_multiplier, raw_material_price FROM [radiogates].[dbo].[ops_invoice] where meter_id ='$comm_id' and subaccount_number='$session_id'";$query5 = sqlsrv_query($conn, $sql5);if ($query5 === false){ exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row5 = sqlsrv_fetch_array($query5)){ $meter_multiplier += $row5['meter_multiplier']; $raw_material_price += $row5['raw_material_price']; $basic_charge += $row5['basic_charge']; $peak_rate += $row5['peak_rate']; $intermediate_rate += $row5['intermediate_rate']; $energy_charge += $row5['energy_charge']; $base_rate += $row5['base_rate']; $account_number = $row5['meter_id']; $service_name = $row5['service_name']; $service_address = $row5['service_address']; $service_address2 = $row5['service_address2']; $service_city = $row5['service_city']; $service_st = $row5['service_st']; $service_zip = $row5['service_zip'];} sqlsrv_free_stmt($query5); ?>Notice: Undefined variable: meter_multiplier in C:\xampp1\htdocs\Utrack\invoice.php on line 335 Notice: Undefined variable: raw_material_price in C:\xampp1\htdocs\Utrack\invoice.php on line 335 Notice: Undefined variable: basic_charge in C:\xampp1\htdocs\Utrack\invoice.php on line 335 Notice: Undefined variable: peak_rate in C:\xampp1\htdocs\Utrack\invoice.php on line 335 Notice: Undefined variable: intermediate_rate in C:\xampp1\htdocs\Utrack\invoice.php on line 335 Notice: Undefined variable: energy_charge in C:\xampp1\htdocs\Utrack\invoice.php on line 335 Notice: Undefined variable: base_rate in C:\xampp1\htdocs\Utrack\invoice.php on line 335 Edited by Butterbean, 10 January 2015 - 06:34 PM. What the hell am i missing here?? Trying to set the $output so if there are any errors it will display a message. If no email or password is entered the $output is echoed fine But if the wrong email or password is entered the $ouptut is not echoed at all??? if (loggedin()){ header ("Location: index.php"); exit(); } if (isset ($_POST['login'])) { $email= $_POST['email']; $password= $_POST['password']; if (isset ($_POST['rememberme'])) {$rememberme= $_POST['rememberme'];} if (isset($rememberme)) {$rememberme= "on";} else {$rememberme= "off";} if (empty($email)) {$output= "<span class='error'>Please enter a username or password</span>";} if (empty($password)) {$output= "<span class='error'>Please enter a username or password</span>";} if ($email&&$password) { $login= mysql_query ("SELECT * FROM user WHERE email='$email'"); while ($row= mysql_fetch_assoc($login)) { $passwordcheck = $row['password']; if (md5($password)==$passwordcheck) $loginok = true; else $loginok = false; if ($loginok==true) { $uniquelogon = "{$row['uniqlogon1']}"; if ($rememberme=="on"){ setcookie("uniquelogon", $uniquelogon, time()+2628000);} else if ($rememberme=="off"){ session_start(); $_SESSION['uniquelogon'] = "$uniquelogon";} header ("Location: index.php"); exit(); } else echo "<span class='error'>Your Email or Password do not match our records</span>"; } } } <?php if (!empty($output)) {echo $output;} ?> <form action"login.php" method="POST"> <p>Email:<br> <input type="text" name="email"> </p> <p>Password:<br> <input id="pwd" type="password" class="required lock pad" watermark="{html:'Password',cls:'pad empty'}" name="password"> </p> <p> <input type="checkbox" name="rememberme"> Remember Me!<br> </p> <input type="submit" name="login" value="Log In"> </form> Ive used all my special powers trying to get this to work, but i am missing something... Is it possible to define a variable's type at the start of your code? I believe you can "cast" a variable, but can I do something like this... Code: [Select] name string; age integer; gender string; children array; single: boolean; I know some people say PHP is better suited for the web because it is loosely-typed, but I'm anal-retentive and like my structure!! TomTees Have a ton of pop-up javascript links in my page. To tidy up the code, I have decided to declare all links at top of the page to keep them together and simply use the variable within the "a" brackets. Let me show you: Code: [Select] <? $brand_edit = 'javascript:void(0)"onclick="window.open('/admin/form.php?db=outlet&tbl=brands&auto_increment=<? echo $auto_increment; ?>&action=edtamp;step=1','none','width=750,height=250,menubar=no,status=no,resizable=no,location=no,toolbar=no,scrollbars=yes,left=50,top=50,titlebar=no')'; ?> <span class="float" style="width: 100px;"><a href="<? echo $brand_edit; ?>"><? echo $auto_increment; ?></a></span> However, something things to go wrong as I'm getting an "Parse error: syntax error, unexpected '=' in ... on line 12" Where am I going wrong with my syntax? My login script stores the user's login name as $_SESSION[ 'name'] on login. For some unapparent reason, i'm getting errors stating that $user and $priv are undefined variables, though I've attempted to define $user as being equal to $_SESSION['name'], using $user to look up the the user's privilege level (stored as the su column ) in the SQL table, and then where the result of the sql query is $priv which is then evaluated in an if statement. I can't seem to figure out why this might not be working. The code I'm using: <?php session_start(); function verify() { //verify that the user is logged in via the login page. Session_start has already been called. if (!isset($_SESSION['loggedin'])) { header('Location: /index.html'); exit; } //if user is logged in, we then lookup necessary privleges. $_SESSION['name'] was written with the login name upon login. Privleges // are written in db as a single-digit integer of of 0 for users, 1 for administrators, and 2 for special users. $user === $_SESSION['name']; //Connect to Databse $link = mysqli_connect("127.0.0.1", "database user", "password", "database"); if (!$link) { echo "Error: Unable to connect to MySQL." . PHP_EOL; echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; exit; } //SQL Statement to lookup privlege information. if ($result = mysqli_query($link, "SELECT su FROM accounts WHERE username = $user", MYSQLI_STORE_RESULT)) { //LOOP TO CYCLE THROUGH SQL RESULTS AND STORE Privlege information as vairable $priv. while ($row = $result->fetch_assoc()) { $priv === $row["su"]; } } // close SQL connection. mysqli_close($link); // Verify privleges and take action. Only a privlege of "1" is allowed to view this page. A privlege of "2" indicates special //accounts used in other scripts that have certain indermediate additional functions, but are not trusted administrators. if ($priv !== 1) { echo $_SESSION['name']; echo "you have privlege level of $priv"; echo "<br>"; echo 'Your account does not have the privleges necessary to view this page'; exit; } } verify(); ?>
Hello everyone, I can get Test 2 to successfully operate the if statement using a variable variable. But when I try the same method using a session variable (Test 1) the if statement is not executed. Please could you tell me why the if statement in Test 1 is not being executed? Code: [Select] <?php # TEST 1 $_SESSION[test_variable] = "abcd"; $session_variable_name = "_SESSION[test_variable]"; if ($$session_variable_name == "abcd") { echo "<br>line 373, abcd<br>"; } # TEST 2 $test_variable = "efgh"; $test_variable_name = "test_variable"; if ($$test_variable_name == "efgh") { echo "<br>line 379, efgh<br>"; } ?> Many thanks, Stu hi all , i am working on a script which is oop driven and i m not much familiar with it, i appericiate if someone can help me to solve this problem , so basicaly current script is only setting one session variable to true if user login $_SESSION['is_successful_login'] , here is my code <?php include('files/db.php'); class ajaxLoginModule { private $timeout = null; private $target_element = null; private $wait_text = null; private $form_element = null; private $wait_element = null; private $notify_element = null; function __construct() { include ('config.php'); $msql = new Db; $msql->connect(); $this->is_login(); } function get_config() { $this->set_ajax_config(); } function set_ajax_config() { $this->timeout = AJAX_TIMEOUT; $this->target_element = AJAX_TARGET_ELEMENT; $this->wait_text = AJAX_WAIT_TEXT; $this->wait_element = AJAX_WAIT_ELEMENT; $this->notify_element = AJAX_NOTIFY_ELEMENT; $this->form_element = AJAX_FORM_ELEMENT; } function initLogin($arg = array()) { $this->get_config(); $this->login_script(); } function initJquery() { return "<script type='text/javascript' src='files/jquery-1.3.2.min.js'></script>"; } function login_script() { include ('files/login_script.php'); } function is_login() { if(isset($_POST['username'])) { $username = $_POST['username']; $password = $_POST['password']; $strSQL = "SELECT * FROM ".USERS_TABLE_NAME." WHERE username ='$username' AND password = '$password' "; $result = mysql_query ($strSQL); $row = mysql_fetch_row($result); /* //THIS IS WHAT I NEED $_SESSION['user'] = $row['username']; $_SESSION['id'] = $row['id']; */ $exist = count($row); if($exist >=2) { $this->jscript_location(); } else { $this->notify_show();} exit; } } function notify_show() { echo "<script>$('.".AJAX_NOTIFY_ELEMENT."').fadeIn();</script>"; } function jscript_location() { $this->set_session(); echo "<script> $('#container').fadeOut();window.location.href='".SUCCESS_LOGIN_GOTO."'</script>"; } function set_session() { session_start(); $_SESSION['is_successful_login'] = true; } } ?> i comment that line what i need is username and id to store in those session variables $_SESSION['user'] = $row['username']; $_SESSION['id'] = $row['id'] i tried to add code in function set_session but did not helped, appreciate for any help. Thanks Need help declaring some session variable guys. I have a login form where the member enters his 1. Pilot Callsign 2. Password I want to declare that Pilot Callsign as the session variable on authentication. Using that Pilot Callsign session variable, I will fetch data from the database relevant to his profile. I already have the whole login page coded along with the restricted access pages (not coded by me). Check this out 1. Page is coded like this and working PERFECTLY --- Code: [Select] <?php // *** Validate request to login to this site. if (!isset($_SESSION)) { session_start(); } $loginFormAction = $_SERVER['PHP_SELF']; if (isset($_GET['accesscheck'])) { $_SESSION['PrevUrl'] = $_GET['accesscheck']; } if (isset($_POST['pilot_callsign'])) { $loginUsername=$_POST['pilot_callsign']; $password=$_POST['password']; mysql_select_db($database_brn_system, $brn_system); $LoginRS__query=sprintf("SELECT pilot_callsign, password, staff_level, firstname FROM pilots WHERE activated = 1 AND pilot_callsign=%s AND password=%s", GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); $LoginRS = mysql_query($LoginRS__query, $brn_system) or die(mysql_error()); $loginFoundUser = mysql_num_rows($LoginRS); if ($loginFoundUser) { $loginStrGroup = mysql_result($LoginRS,0,'staff_level'); if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();} //declare two session variables and assign them $_SESSION['MM_Username'] = $loginUsername; $_SESSION['MM_UserGroup'] = $loginStrGroup; ?> --- 2. As you can see, there already is a session variable declared for Pilot Callsign But on the next page "Restricted Access Page", when I try to call this same Session Variable, it doesn't work. I tried doing this <?php echo $_SESSION['MM_Username'] ?> Moreover, I even tried to fetch data from the table like this - SELECT * FROM pilots WHERE pilot_callsign=$_SESSION['MM_Username'] Doesn't work I wonder whether someone can help me please. I'm using the script below to create a page whereby users are presented with a list of image folders they have created. Clicking on any of the folders allows the user to drill down and view the individual images. Code: [Select] <?php session_start(); $_SESSION['username']=$_POST['username']; $_SESSION['locationid']=$_POST['locationid']; ?> <!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"> <?php //This variable specifies relative path to the folder, where the gallery with uploaded files is located. $galleryPath = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/'; $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; $descriptions = new DOMDocument('1.0'); $descriptions->load($absGalleryPath . 'files.xml'); $items = array(); for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) { $xmlFile = $descriptions->documentElement->childNodes->item($i); $path = $xmlFile->getAttribute('name'); $path = explode('/', $path); $t = &$items; for ($j = 0; $j < count($path); $j++) { if (empty($t[$path[$j]])) { $t[$path[$j]] = array(); } $t = &$t[$path[$j]]; } $t['/src/'] = $xmlFile->getAttribute('source'); $t['description'] = $xmlFile->getAttribute('description'); $t['size'] = $xmlFile->getAttribute('size'); } $basePath = empty($_GET['path']) ? '' : $_GET['path']; if ($basePath) { $basePath = explode('/', $basePath); for ($j = 0; $j < count($basePath); $j++) { $items = &$items[$basePath[$j]]; } } $files = array(); $dirs = array(); function urlpartencode(&$item, $index) { $item = rawurlencode($item); } foreach ($items as $key => $value) { if (isset($value['/src/'])) { $value['/src/'] = explode('/', $value['/src/']); array_walk($value['/src/'], 'urlpartencode'); $value['/src/'] = implode('/', $value['/src/']); $files[] = array( 'name' => $key, 'src' => $value['/src/'], 'description' => htmlentities($value['description'], ENT_COMPAT, 'UTF-8'), 'size' => htmlentities($value['size'], ENT_COMPAT, 'UTF-8') ); } else { $dirs[] = $key; } } $basePath = empty($_GET['path']) ? '' : $_GET['path']; $up = dirname($basePath); if ($up == '.') { $up = ''; } sort($files); sort($dirs); ?> <head> <title>View Image Folders</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link href="Styles/style.css" rel="stylesheet" type="text/css" /> <script src="Libraries/jquery/jquery-1.4.3.min.js" type="text/javascript"></script> <style type="text/css"> <!-- .style1 { font-size: 14px; margin-top: 5px; margin-right: -50px; } --> </style> <body style="font-family: Calibri; color: #505050; margin-right: 160px; margin-left: -180px;"> <div align="right" class="style1"> <a href = "index.php" /> Add Images <a/> → <a href = "javascript:document.imagefolders.submit()"> View All Images </a> </div> <form id="imagefolders" name="imagefolders" class="page" action="gallery.php" method="post" enctype="application/x-www-form-urlencoded"> <div id="container"> </div> <div id="center"> <div class="aB"> <div class="aB-B"> <?php if ('Uploaded files' != $current['title']) :?> <?php endif;?> <div class="demo"> <input name="username" type="hidden" id="username" value="IRHM73" /> <input name="locationid" type="hidden" id="locationid" value="1" /> <div class="inner"> <div class="container"> <div class="gallery"> <table class="gallery-link-table" cellpadding="0" cellspacing="0"> <thead> <tr class="head"> <th class="col-name"> Name </th> <th class="col-size"> Size </th> <th class="col-description"> Description </th> </tr> </thead> <tbody> <tr class="directory odd"> <td class="col-name"> <a href="?path=<?php echo rawurlencode($up); ?>">..</a> </td> <td class="col-size"> </td> <td class="col-description"> </td> </tr> <?php $i = 1; ?> <?php foreach ($dirs as $dir) : ?> <tr class="directory <?php $i++; echo ($i % 2 == 0 ? 'even' : 'odd'); ?>"> <td><a href="?path=<?php echo rawurlencode(($basePath ? $basePath . '/' : '') . $dir); ?>"><?php echo htmlentities($dir, ENT_COMPAT, 'UTF-8'); ?></a></td> <td>Folder</td> <td></td> </tr> <?php endforeach; ?> <?php foreach ($files as $file) : ?> <tr class="<?php $i++; echo ($i % 2 == 0 ? 'even' : 'odd'); ?>"> <td><a target="_blank" href="<?php echo $galleryPath . $file['src']; ?>"><?php echo htmlentities($file['name'], ENT_COMPAT, 'UTF-8'); ?></a></td> <td><?php echo htmlentities($file['size'], ENT_COMPAT, 'UTF-8'); ?></td> <td><?php echo htmlentities($file['description'], ENT_COMPAT, 'UTF-8'); ?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </form> </body> </html> I can create the list of folders, but when I click on any of these, instead of being able to view the images, I receive the following error: Quote Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "/homepages/2/d333603417/htdocs/development/UploadedFiles/files.xml" in /homepages/2/d333603417/htdocs/development/imagefolders.php on line 16 Warning: Invalid argument supplied for foreach() in /homepages/2/d333603417/htdocs/development/imagefolders.php on line 52 Line 16 is this line Code: [Select] $descriptions->load($absGalleryPath . 'files.xml'); and line 52 is this Code: [Select] foreach ($items as $key => $value){ However, if I change this line Code: [Select] $galleryPath = 'UploadedFiles/' . $_SESSION['username'] . '/' . $_SESSION['locationid'] . '/'; to Code: [Select] $galleryPath = 'UploadedFiles/' . 'IRHM73' . '/' . '1' . '/'; i.e. replacing the 'Session Variables' with the actual values, the page works. I've been working on this for days now, and I just can't find the solution. I just wondered whether someoen could perhaps have a look at this and let me know where I'm going wrong. Many thanks and regards Hi I am using very simple code. Here it is Code: [Select] <?php session_start(); $user = "guest"; $uid = "1"; echo $_SESSION['user']."<br />"; echo $_SESSION['uid']; ?> it displays this error Code: [Select] Notice: Undefined index: user in C:\wamp\www\DealDash\index.php on line 5 Notice: Undefined index: uid in C:\wamp\www\DealDash\index.php on line 6 how can I solve this problem? Help please Hey guys, please tell me why the session var $_SESSION['return_url'] is not carrying from test.php to login.php The two echos in this file work perfectly, so i know the session var is registered... test.php <!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" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>SWG:ANH • Bringing PreCU back to life...</title> <link rel="shortcut icon" href="favicon.ico" /> <link rel="stylesheet" type="text/css" media="screen" href="theme/main.css" /> <?php session_start(); // This section will generate a var containing the current page URL. This will be used to allow the language script to redirect users back to the page they // were on, in the language they selected. It is registered as a session variuable that will change every time the page changes. function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } // Here we register the URL we grabbed earlier in the session var. $return_url = curPageURL(); $_SESSION['return_url'] = $return_url; echo $_SESSION['return_url']; echo $return_url; include 'lang/enus.php'; include 'menu.php'; ?> login.php <?php session_start(); echo $_SESSION['return_url'] ; include 'dbconnect.php'; $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $return_url = $_SESSION['return_url']; echo $return_url; $result = mysql_query("SELECT COUNT(*) FROM account WHERE Username='$username' and Pass='$password'") or die(mysql_error()); $result = mysql_fetch_row($result); $result = $result[0]; if($result == 1){ $_SESSION['username'] = $username; header("Location:".$return_url); } else { echo "Wrong Username or Password. Use the back button. If you think this message is incorrect, contact the webmaster."; } ?> I know its not carrying because the echo in login.php is not returning anything and the script cant execute the header function because it has nowhere to go! Ive narrowed it down to the session var not carrying. Hello i have problem in set session variables in .htaccess file. i have tried some codes which are belows but i did not effect in php info file. <IfModule mod_php5.c> php_value session.gc_maxlifetime 86400 php_value session.cache_expire 86400 </IfModule> please let me know if any one have solutions fot this Is it good practice to put an oop object in session variable Question about variable variables and using session variables for them. O.K. So if I have: $foo = 3; $_SESSION['bar'] = "foo"; $$_SESSION['bar'] should equal the value of $foo, however I can't get it to work. Can someone tell me what I am doing wrong..... is it formatting??? Thanks, Thomas I have a session variable called $_SESSION['patchurl'] in a php file , if i get in to an else statement this session variable gets set and i go to http://yyy page. below is the snippet of the code <?php session_start(); ?> <?php echo '<script type="text/javascript">' . "\n"; if(isset($_SESSION["Email"])){ echo 'window.location="http://www.xxx";'; } else{ $_SESSION['patchurl'] = "true"; echo 'window.location="http://yyy";'; } echo '</script>';?>once the patchurl session variable is set i call a php file which sets an other session variable called $_SESSION["Email"]. now what happens is the $_SESSION['patchurl'] is gone and ONLY the $_SESSION["Email"] is accessible ...can i not set two session variables? why does creating a new session varible overwrites an other one even though they are called different ? am i doing something wrong ? Edited by Ch0cu3r, 08 September 2014 - 01:05 PM. Hey guys, been awhile since I have been here, but I hit a little issue in reading session data reliably every time. What I am doing is reading the variables and values from a rarurlencoded string. Then putting that into a session array to populate parts of a form and fill in some hidden fields. After the form posts every now and then, about 1 in 50 or so attempts, I have one (always the same one) that just vanishes. Anyone ever experience anything like this? Hi all - hoping somebody can see this clearer than me... Goal: to create a very simple shopping cart page that receives $_POST info and stores it as an array in a session variable so items will be displayed in cart on clients return to shopping cart page. Problem: I can get the array to display the first item that I add to the shopping cart but each susequest item just overwrites the first because it is using the same session variable name. How do I store the next product info in a DIFFERENT session variable and display it on the shopping cart page without writing over the first? I have two pages with products on them which need to submit orders to the shopping cart page. They use a simple form with these fields: <form method="post" action="cartaction.php"> <p> <input type="submit" name="submit" value="Buy" /> <input type="hidden" name="cartaction" value="add" /> <input type="hidden" name="item" value="coppercasserole" /> </p> </form> This is what I have so far on the cartaction page which can display the first product added to the cart: <?php //If form is submitted, call the function and save all data into the array $_SESSION['form'] if($_POST['submit'] == "Buy"){ setSessionVars(); } function setSessionVars() { if ( !isset($_SESSION['cart']) ) $_SESSION['cart'] = array(); $item = array(); foreach($_POST as $fieldname => $fieldvalue) { $item[$fieldname] = $fieldvalue; } $_SESSION['cart'] = $item; echo "<table> <tr> <td>" .'<img src="images/'.$item['item'].'.jpg"' . "<td/> <td>" . $item['item'] . "</td> <td>" . '<input type="text(30)" name="value" value="1" /> <input type="submit" name="puchasednum" value="Update This One Item" />' . "</td> </tr> </table>"; } ?> I am new to PHP and have hit a brick wall as to how to display each product array as a new session variable in the shopping cart. I would like to keep it as simple as possible as I am getting easily confused! Is there a way that I can increment the session variable with something like this: <?php //session counter $counter = count($_SESSION['cart']); if (($_POST['name'] !="")){ $counter = $counter + 1; $_SESSION['cart'][$counter] = array(); } ?> Or can I pass a field as a product ID and use this somehow to create a new session variable for each product or clear the post variables somehow? Any help would be much appreciated? Thanks The first page is used to submit data to second page (and also show errors if there are any with the data submitted). The second page should be retrieving the posted data and assigning that data to session should it need to refresh back to the first page to show errors. The session data is used to repopulate the form. Right now I'm typing test into the review_name variable and when the page redirects back to the form to display the error messages, that field is not populated with "test". Code: [Select] $product_id=$_GET['product']; session_start(); $error=$_SESSION['error']; $content.='<div class="product_information_text review_form"> <div class="review_header">Write a Review for '.$product_name.'</div> <form action="./review_process.php?product='.$product_id.'&p=php" method="POST"> <p class="form_item"><label>Name:</label> <input type="text" name="review_name" size="30"'; if(isset($_SESSION['review_name'])){$content.=' value="'.$_SESSION['review_name'].'"';} $content.=' />'; if($error[0]=="1"){$content.=' <span class="red">This field is required.</span>';} $content.=' </p> <p class="form_item"><label>E-Mail:</label> <input type="text" name="review_email" size="30"'; if(isset($_SESSION['review_email'])){$content.=' value="'.$_SESSION['review_email'].'"';} $content.=' />'; if($error[2]=="1"){$content.=' <span class="red">This field is required.</span>';} $content.=' </p> <p class="form_item"><label>Location:</label> <input type="text" name="review_location" size="30"'; if(isset($_SESSION['review_location'])){$content.=' value="'.$_SESSION['review_location'].'"';} $content.=' />'; if($error[3]=="1"){$content.=' <span class="red">This field is required.</span>';} $content.=' </p> <p class="form_item"><label>Describe Yourself:</label> <input type="text" name="review_describe" size="30"'; if(isset($_SESSION['review_describe'])){$content.=' value="'.$_SESSION['review_describe'].'"';} $content.=' />'; if($error[4]=="1"){$content.=' <span class="red">This field is required.</span>';} $content.=' </p> <p class="form_item"><label>Review Title:</label> <input type="text" name="review_title" size="30"'; if(isset($_SESSION['review_title'])){$content.=' value="'.$_SESSION['review_title'].'"';} $content.=' />'; if($error[1]=="1"){$content.=' <span class="red">This field is required.</span>';} $content.=' </p> <p class="form_item"><label>Best Use of Product:</label> <input type="text" name="review_best_use" size="30"'; if(isset($_SESSION['review_best_use'])){$content.=' value="'.$_SESSION['review_best_use'].'"';} $content.=' />'; if($error[5]=="1"){$content.=' <span class="red">This field is required.</span>';} $content.=' </p> <p class="form_item"><label>Product Pros:</label> <input type="text" name="review_pros" size="30"'; if(isset($_SESSION['review_pros'])){$content.=' value="'.$_SESSION['review_pros'].'"';} $content.=' />'; if($error[6]=="1"){$content.=' <span class="red">This field is required.</span>';} $content.=' </p> <p class="form_item"><label>Product Cons:</label> <input type="text" name="review_cons" size="30"'; if(isset($_SESSION['review_cons'])){$content.=' value="'.$_SESSION['review_cons'].'"';} $content.=' />'; if($error[7]=="1"){$content.=' <span class="red">This field is required.</span>';} $content.=' </p> <p class="form_item"><label>Product Rating:</label><br /> <div class="rating_radio"><input type="radio" name="review_product_rating" value="1"'; if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="1"){$content.='checked';} $content.=' /> <br />1</div> <div class="rating_radio"><input type="radio" name="review_product_rating" value="2"'; if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="2"){$content.='checked';} $content.=' /> <br />2</div> <div class="rating_radio"><input type="radio" name="review_product_rating" value="3"'; if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="3" || !isset($_SESSION['review_product_rating'])){$content.='checked';} $content.=' /> <br />3</div> <div class="rating_radio"><input type="radio" name="review_product_rating" value="4"'; if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="4"){$content.='checked';} $content.=' /> <br />4</div> <div class="rating_radio"><input type="radio" name="review_product_rating" value="5"'; if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="5"){$content.='checked';} $content.=' /> <br />5</div> <div class="worst">(Worst)</div><div class="best">(Best)</div> </p> <p> </p> <p class="form_item"><label>Comments on Product:'; if($error[7]=="1"){$content.=' <span class="red">This field is required.</span>';} $content.=' </label><br /> <textarea name="review_text" rows="10" cols="60">'; if(isset($_SESSION['review_text'])){$content.=$_SESSION['review_text'];} $content.='</textarea> </p> <p><input type="submit" value="Submit" name="Submit" /></p> </form> </div> '; session_unset(); session_destroy(); Code: [Select] session_start(); $product_id=$_GET['product']; $review_name=$_POST['review_name']; $_SESSION['review_name']==$review_name; $review_name = stripslashes($review_name); $review_name = mysql_real_escape_string($review_name); if($review_name==""){ $error0=1; } else{ $error0=0; } $review_title=$_POST['review_title']; $_SESSION['review_title']==$review_title; $review_title = stripslashes($review_title); $review_title = mysql_real_escape_string($review_title); if($review_title==""){ $error1=1; } else{ $error1=0; } $review_email=$_POST['review_email']; $_SESSION['review_email']==$review_email; $review_email = stripslashes($review_email); $review_email = mysql_real_escape_string($review_email); if($review_email==""){ $error2=1; } else{ $error2=0; } $review_location=$_POST['review_location']; $_SESSION['review_location']==$review_location; $review_location = stripslashes($review_location); $review_location = mysql_real_escape_string($review_location); if($review_location==""){ $error3=1; } else{ $error3=0; } $review_describe=$_POST['review_describe']; $_SESSION['review_describe']==$review_describe; $review_describe = stripslashes($review_describe); $review_describe = mysql_real_escape_string($review_describe); if($review_describe==""){ $error4=1; } else{ $error4=0; } $review_best_use=$_POST['review_best_use']; $_SESSION['review_best_use']==$review_best_use; $review_best_use = stripslashes($review_best_use); $review_best_use = mysql_real_escape_string($review_best_use); if($review_best_use==""){ $error5=1; } else{ $error5=0; } $review_pros=$_POST['review_pros']; $_SESSION['review_pros']==$review_pros; $review_pros = stripslashes($review_pros); $review_pros = mysql_real_escape_string($review_pros); if($review_pros==""){ $error6=1; } else{ $error6=0; } $review_cons=$_POST['review_cons']; $_SESSION['review_cons']==$review_cons; $review_cons = stripslashes($review_cons); $review_cons = mysql_real_escape_string($review_cons); if($review_cons==""){ $error7=1; } else{ $error7=0; } $review_product_rating=$_POST['review_product_rating']; $_SESSION['review_product_rating']=$review_product_rating; $review_product_rating = stripslashes($review_product_rating); $review_product_rating = mysql_real_escape_string($review_product_rating); $review_text=$_POST['review_text']; $_SESSION['review_text']==$review_text; $review_text = stripslashes($review_text); $review_text = mysql_real_escape_string($review_text); if($review_text==""){ $error8=1; } else{ $error8=0; } $review_show="n"; date_default_timezone_set('US/Eastern'); $review_date = date("F j, Y, g:i a T"); $error="".$error0."".$error1."".$error2."".$error3."".$error4."".$error5."".$error6."".$error7."".$error8.""; if($_GET['p']=="php"){ if($error!=="000000000"){ $_SESSION['error']=$error; //header("Location: ./store.php?product=".$product_id."&write=review"); echo $_SESSION['review_name']; } else{ $sql="INSERT INTO $tbl_name3 (product_id, review_show, review_title, review_email, review_name, review_location, review_date, review_describe, review_best_use, review_pros, review_cons, review_product_rating, review_text) VALUES ('$product_id', '$review_show', '$review_title', '$review_email', '$review_name', '$review_location', '$review_date', '$review_describe', '$review_best_use', '$review_pros', '$review_cons', '$review_product_rating', '$review_text')"; mysql_query($sql); header("Location: ./store.php?product=".$product_id."&reviews=thankyou"); } } On this second page echo $_SESSION['review_name']; returns nothing, when changed to $_SESSION['review_product_rating']; it returns the rating I selected in the form. I'm probably missing something obvious here. |