PHP - Some Advice On Using Sessions
Was wondering if anyone had any thoughts on how im running settings on my text base game.
I currently have like 60 settings stored in the data base. These are only admin control settings. They set what things cost in the game and such. I was thinking today that would it be better instead of running a query on every page to grab the settings that page uses. I just simply do one huge select ALL query for the settings when a user logs in and turn all the setting into super global $_SESSION. Also this way any function i made i can simply use the SESSION instead of grabing settings out side the function or creating a query for it with in the function. So i was thinking doing this would extremly speed up my game wouldnt it? wondering if anyone has any thoughts on this Similar TutorialsI have a form that multiple users are accessing at the same time. Within the form is a project number field. The project number is generated from a function that queries the project table in the database for the maximum project number then adds 1. It's starting to be a problem where multiple users access the form and are given the same project number. The form doesn't insert into the table, instead an excel spreadsheet is generated with the form info and the user emails it to someone else for data entry. Is there a way that I can resolve my issue so that each time someone accesses the form no one receives the same project number? So here's my problem I'm not sure how to approach this: I have a table with user_items which are stored together separated by commas. Code: [Select] 13,12,11,9,27,15,16,22,21,23,24,26,29,30,31,32,33 Now, I have a script where the user is in a trade and I want to verify the item they are trying to trade, but is there an alternative other than grabbing all of that users' items and checking that one item with all of the records? I've tried using Code: [Select] SELECT * FROM MYTABLE WHERE user_item_id IN(33) As an example to see if it will pull the rows with that ID. It didn't seem to work, am I doing it wrong? if so, forgive me. Any suggestions/help? The main problem is I don't want to have to explode that data and use a foreach to check that one item against all of that users items, as they could have well over 500. In short i want to use the following code below, when someone selects there option and submits it, it would bring up details from the database on this user from the selected table, can you explain what it would be called doing this so i can look it up, Sorry to be a pain, Cheers. Code: [Select] <select name="target2" id="target2"> <option value=""></option> <?php $sql = "SELECT player_id, friend_id, name, is_active FROM contacts as c JOIN players as p ON c.friend_id = p.id WHERE c.player_id = $playerID AND is_active = 1 ORDER BY name ASC"; $que = mysql_query($sql) or die(mysql_error()); while($list = mysql_fetch_array($que)) { ?> <option value="<?php echo $list['friend_id'] ?>"><?php echo $list['name'] ?></option> <?php } ?> </select> Right now I have a SESSION so when users flip though pages they carry their info with them, what I'm trying to do now is that userhome.php can't be accesses unless the user just was succesful in cracking there system.. game I'm creating for those of you helping and following me while I do this! it's a virutal hacking simulation and where I'm now is that the user's passwordcracker was compared to the target systems 'systemkey' and either granted him access or didn't, if it did it displayed a progress bar then fowarded to userhome.php where the target users info will lay, right now though if I just type in userhome.php i get there without haveing the crack it.... any ideas? for those of you who don't know i am creating a piece of forum software called ASF. Ive done it by myself so far but as it grows i find it harder to write the code and keep organised. my code is a mess and things arent done the way they should be. So if anyone can give me advice or wants to help i could post some of the files for download. Even if you just want to have a look and let me know waht you think. Thanks Carl http://www.thevault.cz.cc Hi all, I am looking as a pet project to develop a review site, with the info stored in a database by id and the information grabbed bet get id and then displayed on a dynamic page, eg review.php?id=1 My question is this, if i throw keywords into the mix for each review, will search engines cache a review like this? Or would I need static pages for google etc to find the info? Thanks Hello, can someone please advice me on what scripts I will need to accomplish the following. I want users to be able to login to their personal page, on there will be items such as pdf files, jpeg files etc, that they will be able to download. Are there any free scripts out there that can do this, that anyone knows of? I don't mind paying if its a cheapish script for one of you to make for me, but money is a bit tight at the moment so a free script would be my 1st choice... Thanks for all your help Hi, basically i have data in my database i want to represent as cash, i currently put the dollar sign infront of each echo which is fine, but how would i go about adding , to the php code itself as you cannot do this from the sql database.. I haven't used sessions much until now, so this is probably due to my ignorance. I have a page that sets the session variable, and if I print from that page, the session variable (an array) is correct. But when I move to the next page, the same session variable has old, old, wrong data. The $arrAttendeeList is an exploded list from a textarea on a form turned into an array. Example: This page, let's call it page1.php, sets the variable: Code: [Select] <?php $_SESSION['arrAttendeeList'] = $arrAttendeeList; foreach ($_SESSION['arrAttendeeList'] as $temp) { print "$temp <br />"; } exit; ?> Results: Smithers, Waylon Bouvier, Selma Brockman, Kent But the next page, page2.php, when I call the same variable: Code: [Select] <?php foreach ($_SESSION['arrAttendeeList'] as $temp) { print "<br />$temp <br>"; } ?> I get yesterday's data: Smithers, Waylon Bouvier, Selma I've tried setting the session var to null but with the same results. Do I need to kill the session var before setting it to something else? Thanks - I got a log-in form with database, it only logs if such username and password exists. I was told that if i wanna make a log-out button once logged-in, i need to add sessions to my code and idk how.. this is my code that checks if username/password exists, and if so it lets u log in, if not it displays a msg: if(isset($_POST['loginsubmit'])){ if($username !="" && $password !="") { ///////////////////////////////Check for username/pass in database//////////////////////////// $nameexists = false; $passexists = false; $result = pg_query("SELECT name FROM duom WHERE name='".$username."'"); while ($row = pg_fetch_array($result)) { if($row['name'] != ""){ $nameexists = true; } } if($nameexists) { $result = pg_query("SELECT pass FROM duom WHERE name='".$username."'"); while ($row = pg_fetch_array($result)) { if($row['pass'] == $password){ $passexists = true; echo "Prisijungimas pavyko, jusu vartotojo vardas - ".$username.""; include"loggedinform.php"; } else { echo "Slaptazodis netinka!"; } /////////////////Starts session if password is correct//////////////////////////////////////////// if ($passexists){ //Here i want it to start the session if password is correct } ////////////////////////////////////////////////////////////////////////////////////////////////////////// } } else { echo "Tokio vartotojo nera!"; } ////////////////////////////////////////////////////////////////////////////////////////////////////////// } else { echo "Uzpildykite visus duomenys!"; } } Cant i just make smth like this? if ($passexists){ session_start(); } Hi, I have been looking at this code for hours and I can't figure out why the product name and quantity aren't transferring from treats.php to checkOut.php. I realize checkOut.php is extremely long but if I could get some help I would really appreciate it. In case you would want the link, it 's http://auntievics.com/treats.php i have an upload form and a posting form on the same page. when you upload a file it is uploaded to the server. what i am then trying to do is add the name of the file to $_SESSION['attachment'] so i can use it later. When the user posts their form i want the session to be inserted into the database but the session always comes up empty. this is what happens when they upload their file Setting the session and moving the file: session_start(); $_SESSION['attachment'] = "EXAMPLE"; move_uploaded_file($_FILES['Filedata']['tmp_name'], "../attachments/" . time() . $_FILES['Filedata']['name']); and then when they submit their form(textarea) it uploads the contents to the database and the contents of the session aswell. Why is this session always empty? hey on my page a list of people get listed by a category u choose if u choose a category and click ok, u are sent to the same page, and i get the category with POST if(isset ($_POST['type'])) { $gameholder = $_POST['game']; $typeholder = $_POST['type']; } this works ... the list of people is 30 members / page when u click on page 2, the post doesnt work anymore, and i get errors, logic ... now i want to get around this with making a session emediatly when the vars get out of POST if(isset ($_POST['type'])) { $gameholder = $_POST['game']; $typeholder = $_POST['type']; $_session['gameholder'] = $gameholder; $_session['typeholder'] = $typeholder; print $_session['gameholder']; } this also works now i want when people click on page 2, the needed vars will come out of the Sessions, and not out of POST so i did this if(!isset ($_POST['type'])) { $gameholder = $_session['gameholder']; $typeholder = $_session['typeholder']; } but this is not working theres nothing in $gameholder also when i move the print session out of the if, it should show when someone clicks page 2 but its not ... any suggestions on this? thanks! I am new to SESSIONS and have a quick question about them. I want to use sesssions on my site but was wondering if they would work for the follow senario. Say a customer visits my url: http://www.mysite.com/?id=2 Now what i am doing is taking the id out of the URL using sessions and redirecting the user to http://www.mysite.com while my session ($_SESSION['id']) holds the value 2 in it. I have this working great, i believe it just looks better. Now if my customer decides to buy my product via paypal and is directed off my site while he/she is paying for said item on paypal when they return could i still use some of the information that i stored in my session?? I know i could use post and get to pass through paypal but unfortunately i have to use sessions for what i am trying to do. Hello everyone, By default when someone comes to my site, it is the unencrypted web page (http://www.mydomain.com). I recently purchased a ssl certificate so when a user logs in, their password is encrypted (Posted to https://mydomain.com). Once the form is posted, the session is set and the header("Location: http://www.mydomain.com"); function is executed to bring them back to the unencrypted version of the site. However, now when a user tries to login, my site seems to drop the session when the location is set to the unencrypted version of the site. How do sites go about using a secure login form and running on the regular version of the site? Thanks! weee So I'm trying to understand Sessions and how to store a variable within a session. What I want to t do, is start a session, check if variable is set, if not, set the variable. So with the code below, I start the session, i check the variable, if not set, i set it. But when i refrsh the page, it has the same session id but it didn't store the session variable from the previous load. Please, what am I missing? Code: [Select] <?php session_start(); echo "Session ID: ".session_id()."<br>"; echo "<br>chktrack P ".$_session['chktrack'].""; if ($_session['chktrack'] != 1){ $_session['chktrack']=1; } echo "<br>chktrack Post: ".$_session['chktrack'].""; echo "<br><a href='index.php'>Index</a>"; ?> Thanks in advance for your help. I have an "Email a Friend" form that captures the referring url and saves it in a session. The problem is that if someone decides not to send the form and navigates away from the page, and then decides to email another page, it saves the previous url. How can I have it so that it clears the previous url and uses the new one? Code: [Select] if(!isset($_SESSION['referrer'])){ //get the referrer if ($_SERVER['HTTP_REFERER']){ $referrer = $_SERVER['HTTP_REFERER']; } else{ $referrer = "http://www.mcse-training-classes.com"; } //save it in a session $_SESSION['referrer'] = $referrer; } I there. I am making a small game, either you or the computer win depending on who's life hit 0 first I am using sessions to hold the health values, however I need a little bit of help. How do I make it actually go down after each move until one hits 0? Here is my script and thanks in advance... Code: [Select] <?php session_start(); ?> <!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>Fighting Game</title> </head> <body> <form action="fighting_game.php" method="post"> <select name='move_choice' id='move_choice'> <option value='punch'>Punch</option> <option value='kick'>Kick</option> </select> <input type='submit' name='submitbtn' value='Continue' id='submitbtn'> </form> <div id='matchdiv'> <?php $_SESSION['ai_health'] = 100; $_SESSION['player_health'] = 100; $moves_ai = array("Computer punches you in the face!","Computer kicks you in the gut!"); $moves_player = array("You punch Computer in the face!","You kick computer in the gut!"); $move_damage = array(2,10); $move_dam_multiplier_player = array(rand(1,5),rand(5,10)); $move_dam_multiplier_ai = array(rand(1,5),rand(5,10)); if($_POST['submitbtn']){ $choice = $_POST['move_choice']; } if($choice == "punch"){ $total_dam_ai=($move_damage[0]*$move_dam_multiplier_ai[0]); $total_dam_player=($move_damage[0]*$move_dam_multiplier_player[0]); echo"$moves_player[1]"." Causing ".$total_dam_player." damage!<br>"; echo"Computers current health is ". ($_SESSION['ai_health']-$move_damage[0]*$move_dam_multiplier_player[0]); echo"<br>$moves_ai[1]"." Causing ".$total_dam_ai." damage!<br>"; echo"Your current health is ". ($_SESSION['player_health']-$move_damage[0]*$move_dam_multiplier_ai[0]); } elseif($choice == "kick"){ $total_dam_ai=($move_damage[1]*$move_dam_multiplier_ai[1]); $total_dam_player=($move_damage[1]*$move_dam_multiplier_player[1]); echo"$moves_player[1]"." Causing ".$total_dam_player." damage!<br>"; echo"Computers current health is ". ($_SESSION['ai_health']-$move_damage[1]*$move_dam_multiplier_player[1]); echo"<br>$moves_ai[1]"." Causing ".$total_dam_ai." damage!<br>"; echo"Your current health is ". $new_player_health=($_SESSION['player_health']-$move_damage[1]*$move_dam_multiplier_ai[1]); } if($_SESSION['ai_health']<=0 && $_SESSION['player_health']>=0){ echo"<br>Computer falls to the ground! He is knocked out! You win!"; } if($_SESSION['player_health']<=0 && $_SESSION['ai_health'] >= 0){ echo"<br>You fall to the ground! You are knocked out! You lose!"; } if($_SESSION['player_health']<=0 && $_SESSION['ai_health'] <=0){ echo"<br>You both fall to the ground! You are both knocked out! It's a draw!"; } ?> </div> </body> </html> I am trying to create a blog and visitor log, where I should be able to log in as admin and add the item and see the visitor log. I want a password protected administration section and I want to use sessions on the administration section. When I for example click on add post, I want it to log in and go to page addpost.php where I can enter my text. This is what I've done so far. I would like to know what I have done wrong and how I should do to accomplish what I have mentioned above. <?php session_start(); if(isset($_POST['btnLogin']) || isset($_POST['addpost'])){ $txtUserId = $_REQUEST['txtUserId']; $txtPassword = $_REQUEST['txtPassword']; if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) { if ($_POST['txtUserId'] === 'admin' && $_POST['txtPassword'] === 'abc123') { //$_SESSION['basic_is_logged_in'] = true; echo "you are logged in"; } elseif (empty($txtUserId) || empty($txtPassword)) { echo "fill in username and password"; } elseif ($_POST['txtUserId'] != 'admin'){ echo "wrong username"; } elseif ($_POST['txtPassword'] != 'abc123'){ echo "wrong password"; } } } ?> <?php //if (!isset($_SESSION['basic_is_logged_in']) // || $_SESSION['basic_is_logged_in'] !== true) ?> <html> <head> <title>Main User Page</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form method="post" name="frmLogin" id="frmLogin" action="main.php"> <table width="400" border="1" align="center" cellpadding="2" cellspacing="2"> <tr> <td width="150">User Id</td> <td><input name="txtUserId" type="text" id="txtUserId"></td> </tr> <tr> <td width="150">Password</td> <td><input name="txtPassword" type="password" id="txtPassword"></td> </tr> <tr> <td><input type="submit" name="btnLogin" value="Login"></td> </tr> <tr> <td><input type="submit" name="addpost" value="add post" /></td> </tr> <tr> <td><input type="submit" name="showstat" value="Show visitors log" /></td> </tr> <tr> <td><input type="submit" name="blogg" value="To blog (logout)" /></td> </tr> </table> </form> </body> </html> |