PHP - Problems With Sessions
Pesky me again.
This time I`d like to have my session code looked over. I`m having some problems with it. When a user logs into my website (provided they enter all the right information), they will log in no problem. However, the session doesn`t last as long as I`d like it and it seems to be unpredictable with how long it`ll last. Such as, one day it`ll last 5 hours even with closing the browser. The next day it might last 20 minutes. The next day it may log the person out if they close the browser. I`d looked around on the net to see what could be going wrong with my code but I guess I`m not getting it. Any more epxerienced set of eyes want to look it over and give me their opinion: function RemoveSession() { $_SESSION = array(); if (isset($_COOKIE[session_name()])) { @setcookie(session_name(), '', time()+(60*60*24*365), '/'); } } Is it even possible to make a session last as long as cookies can? Like have it last a month even though the person closes the browser and shuts off their computer and doesn`t visit for a few days? I`d like to have it start out lasting a full month if possible. Similar TutorialsPlease help, i'm writing a code that a user must match a random word ($word) from a choice of definitions ($word_defs)...No matter what definition is picked from the list, the response is always 'incorrect', can someone please look at my code and tell me what's wrong? Here is the entry code: Code: [Select] require_once "include/word_defs.php"; $word = array_keys($word_defs); $word_defs = array_unique(array_values($word_defs)); $session = new Session(); $params = (object) $_REQUEST; if (isset($params->guess)) { $word = $params->word; if ( $params->word_defs == $word_defs[$word] ) { $response = "correct"; } else { $response = "incorrect"; } $choices->word_def[$params->word_defs] = "selected"; } else { $word = $word[ rand( 0, count($word)-1 ) ]; } Code in body: Code: [Select] word: <?php echo $word ?> <form action="?" method="get"> definition: <select name="word_defs"> <?php foreach ($word_defs as $word_def): ?> <option <?php echo $choices->word_def[$word_def] ?> ><?php echo $word_def?></option> <?php endforeach ?> </select> <input type="submit" name="guess" value="Guess" /> <input type="hidden" name="word" value="<?php echo $word?>" /> </form> <h3><?php echo $response ?></h3> Hello everyone, it's my first post here. I was recommend this community for it's professionalism and "friendlyness". During my stay here, please excuse my english. I know it can be unpleasant for you to read a thread with terrible grammar, I hate it in my native language, I'll do my best. Okay, so I'm having two major issues for my website to operate like I'd like it to, I've done everything I knew about, and I can't think about anything else Google didn't help me much either this time. First one is that I seem to have a cookies/sessions problem. Well, I use Liberty Reserve to allow my members to top up their account. So I send my form to their SCI and when I either cancel or complete a transaction, I return to my website but I'm not logged in anymore! So the auto-add to balance system can't work. Alright, when I saw this I was only using sessions, so I added cookies so I could rely on them too. And there again, same thing. Doubled, tripled checked all the LR settings and everything, I know they are right. But what's really, really, weird to me and what's really doing my head in is that I'm not actually logged out, because if I use another tab/window I'm still logged in. In other words, it's only the window coming back from Liberty Reserve that won't log in. The cookies are still here and so is the session so... Another terrible thing is that it used to work when I was on my previous host (just moved to a dedicated server, which may be causing the problems, it's the first time I administrate a dedi). This is blocking my entire website from working. And my second problem is with the function mail() -there again 'caused' by new server-. I don't get any error message, or anything. The e-mails with cPanel/WHM work great. Though, I have done several tests and it never reaches my Gmail, Hotmail or Yahoo account. It did work with a Yopmail inbox though. So, I was focusing on 'why isn't it sending it' but since I just noticed it worked with Yopmail, I'm sure it is sending it and have no ideas why wouldn't it work with other e-mail service providers. I hope you understood what I meant and that, hopefully, you guys can help me out with those totally handicapping issues. Thank you, Regards. -Ben. I am in the process of opening my web site to the general public. Right now you have to have a username and password to see anything on the site. What I am looking to do is make it so anyone can see most things on the site. There will still be user accounts for things like admins and submitting ideas. What i have now is some links that look at the user rank and only show up if you are higher then a rank. What I need to do is make it so you don't need to log in to see the site but you still need to log in to edit things. Here is my sessions scrip Code: [Select] <?php session_start(); if(!$_SESSION['login']){ $_SESSION['rank']; $_SESSION['loggedinusername'] = $loggedinusername; $_SESSION['loggedinuseremail'] = $loggedinuseremail; header("location:login.php"); } $rank=$_SESSION['rank']; $loggedinusername=$_SESSION['loggedinusername']; $loggedinuseremail=$_SESSION['loggedinuseremail']; ?> How do I edit this so you are free to move around the site even if your not logged in? Thanks When using sessions, must there be a session_start() in each page for the browser to be considered part of the session? Can a user browse to a page without session_start() and still have that part of the original session that he/she began with?? hey i think i may have stored session variables incorrectly Code: [Select] $_SESSION['tel'] = $_GET['Lat']; $_SESSION['Lon'] = $_GET['Lon']; $_SESSION['Lat'] = $_GET['Lat']; is what i used to set the session variables with the data i then enter these into a table and they enter the correct information but two pages down the line i try to access them however i just get undefined variable when i set the variable is equal to the session. Code: [Select] $MyLon = $_SESSION['Lon']; $MyLat = $_SESSION['Lat']; the following is the errorr Quote Notice: Undefined index: Lat in So I am trying to set my session length, but it doesn't seem to be working. session_set_cookie_params("84600"); session_start(); 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! hi im new to php i am making shopping cart i made a session variable to add products in cart when i run it gives this warning Notice: Undefined index: cart in C:\wamp\www\cart\public\views\layouts\shop.php on line 15 Notice: Undefined index: total_items in C:\wamp\www\cart\public\views\layouts\shop.php on line 16 Notice: Undefined index: total_price in C:\wamp\www\cart\public\views\layouts\shop.php on line 17 but when i click on add to cart it add product price and quanitity kindly help me my shop.php code is <head> <title> Shopping Cart</title> <link href="stylesheets/cool_style.css" media="screen" rel="Stylesheet" type="text/css" /> </head> <body> <h1> products</h1> <div class="cart"> <p><b> Shopping Cart</b></p> print_r($_SESSION['cart']); echo $_SESSION['total_items']; echo number_format($_SESSION['total_price'],2); </div> <hr /> </body> </html> and my index file is where i set session variables are include('db_fns.php'); include("cart_functions.php"); session_start(); if(isset($_SEESION['cart'])) // if change this conditon if(!isset($_SEESION['cart'])) it gives not warning but does not //add products to the cart it does not do anything { $_SESSION['cart']=array(); $_SESSION['total_items']=0; $_SESSION['total_price']=0.00; } $view=empty($_GET['view'])?'index':$_GET['view']; $controller='shop'; switch ($view){ case "index"; $products = find_product(); break; case "add_to_cart"; $id=$_GET['id']; $add_item=add_to_cart($id); $_SESSION['total_items']=total_items($_SESSION['cart']); $_SESSION['total_price']=total_price($_SESSION['cart']); header('Location: index.php'); break; case "update_cart"; break; case "checkout"; break; } if change this conditon if(!isset($_SEESION['cart'])) it gives not warning file run correctly with no warning but does not add products to the cart and does not do anything kindly help me please I have a simple php script that starts sessions. On every page, I include : if(isset($_SESSION['sessionname'])){ //The rest of the page } else { die("Not logged in"); } I always include the session start and always regenerate the session id after <?php. The code works fine withevery browser except for a certain version of Internet Explorer 8. Even though the browser does enable cookies, it doesn`t seem to allow the ones in my script. In fact, every time I change page, it ives the error message "Not logged in". I have tried it on two different compters with the exact same version of IE and the result was the same. Thank you for your time! Hi all, If I have a list of session ids, is it possible to use this list to determine which session is no longer active? Seems like it should be do-able but can't find help on it so I'm kinda guessing its not! Thanks for any help, Michael Hello everyone, i'm new to php and i'm having hard time with sessions i'm trying to create a php file with a drop down menu and when you select an item from the drop down menu, you could retreve it from another page. for example: a1.php Code: [Select] <?php session_start(); if(isset($_POST['color'])) { $_SESSION['blue']='blue'; $_SESSION['red']='red'; $_SESSION['green']='green'; $_SESSION['orange']='orange'; } ?> <html> <body> <form id="shirt" method="post" action="a2.php"> <p> <select name="Size"> <option value="invalid">Select a size ...</option> <option value="blue">blue</option> <option value="red">red</option> <option value="green">green</option> <option value="orange">orange</option> </select> <br /> <input type="Submit" value="Add" name="Add" /> </p> </form> </body> </html> when the user chooses a color, it adds it to the session and then when the user clicks add, he is redirected to another page named a2.php which shows the color is added. if the user goes back to the original page and adds the same color again it shows that he added the item again: Color: ----------- Quantity: Red ----------- 2 a2.php Code: [Select] <?php session_start(); $item_id = $_GET[id]; $action = $_GET[action]; switch($action) { case "add": $_SESSION['color'][$item_id]++; break; case "remove": $_SESSION['color'][$item_id]--; if($_SESSION['color'][$item_id] == 0) unset($_SESSION['color'][$item_id]); break; case "empty": unset($_SESSION['color']); break; } ?> sorry if my question is not clear, any help is appreciated Thank You, 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> Kind of a n00b here. on my main table (users) i named a column as "id", set it to auto-increment and as the primary key and created it like this: CREATE TABLE `users` ( `id` int(20) NOT NULL auto_increment, `full_name` varchar(200) collate latin1_general_ci NOT NULL default '', `user_name` varchar(200) collate latin1_general_ci NOT NULL default '', `user_pwd` varchar(200) collate latin1_general_ci NOT NULL default '', `user_email` varchar(200) collate latin1_general_ci NOT NULL default '', `activation_code` int(10) NOT NULL default '0', `joined` date NOT NULL default '0000-00-00', `country` varchar(100) collate latin1_general_ci NOT NULL default '', `user_activated` int(1) NOT NULL default '0', PRIMARY KEY (`id`) ) On the second table i created it like this: CREATE TABLE about_me ( about_id int NOT NULL, nick_name varchar(255), descript varchar(255), aim varchar(255), cell varchar(255), school varchar(255), music varchar(255), aspire varchar(255), City varchar(255), id int, PRIMARY KEY (about_id), FOREIGN KEY (id) REFERENCES users(id) ) I believe i imported the key correctly into my new table (about_me). Well I expected the id column to cascade down into this new table automatically which it didn't. RIght now if you log into my site and use the about me form, it posts to the new table "about_me" but it doesn't identify the user on the table with the primary key assigned to him from the first table (users). How do I use PHP sessions to identify the user by his/her id from the primary key in the table. I attached the whole site. The php for the log in is a prefab and I'm attempting to do the about me part on my own, I'm having alot of trouble with the whole sessions thing. I'm not really sure if I'm doing this correctly. so yeah any point in the right direction would be awesome! -Mike Hi Something strange is happening and I can't understand it. A user can access an availability page of accommodation and book ita room, this works fine, and goes from availability to the booking form and back quite well, carrying the room id of the accommdation and room/s selected in a session. If they close the browser down and open the availability page again all the rooms are there as before, but when they select a room and go to the booking form the session of the room id and the rooms selected are empty. If I do a session destroy and open the browser up again everything works fine again. I have tried this in Chrome and Firefox and it seems to work fine Any help would be appreciated Hi, I have an admin area to update users detials etc.. I want this area to be secure so only admins can access it, Currently anyone and everyone can access the page. Code: [Select] <?PHP session_start(); /* really need to use a session variable to insure authorized to be here */ include ('db.php'); /* ======================================== 99% of the time it is better to put your query in a string. It makes debugging much easier ======================================== */ $query = "SELECT * FROM companies"; $result = mysql_query($query ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); /* ======================================== I find it easier to locate problems if I indent code properly and drop out of PHP if there are large sections of html ======================================== */ ?> <br><br><br>There are <?PHP echo $num_rows; ?> removalspace users so far.<P> <table width="819" height="114"> <tr> <th>Company Name</th> <th>Contact Name</th> <th>Contact Number</th> <th>Email</th> <th>Address Line 1</th> <th>Address Line 2</th> <th>Location</th> <th>Postcode</th> <th>Basic Members</th> <th>Upgraded Users</th> <th>Company Logo</th> <th>Approved</th> </tr> <tr> <td colspan="6"></td> </tr> <?PHP while ( $row = mysql_fetch_array($result, MYSQL_ASSOC )) { ?> <tr> <td><?PHP echo $row['company_name']; ?></td> <td><?PHP echo $row['contact_name']; ?></td> <td><?PHP echo $row['phone']; ?></td> <td><?PHP echo $row['email']; ?></td> <td><?PHP echo $row['street1']; ?></td> <td><?PHP echo $row['street2']; ?></td> <td><?PHP echo $row['location']; ?></td> <td><?PHP echo $row['postcode']; ?></td> <td><?PHP echo $row['basicpackage_description']; ?></td> <td><?PHP echo $row['premiumuser_description']; ?></td> <?PHP /* ======================================== I presume you want to show the thumb version here ======================================== */ ?> <td><img src="images/thumbs/<?PHP echo $row['upload']; ?>" alt="logo"/></td> </tr> <tr> <td colspan="10"> <table> <tr> <td>Current level = <?PHP echo $row['approved']; ?></td> <td><a href="admin02.php?id=<?PHP echo $row['id']; ?>&level=1">Level 1 - Free</a></td> <td><a href="admin02.php?id=<?PHP echo $row['id']; ?>&level=2">Level 2 - Basic</a></td> <td><a href="admin02.php?id=<?PHP echo $row['id']; ?>&level=3">Level 3 - Premium</a></td> <td><a href="admin02.php?id=<?PHP echo $row['id']; ?>&level=0">Level 0 - Do Not Display</a></td> </tr> </table> </tr> <?PHP } echo "</table>"; ?></table> i know it's something like: <?php session_start(); if(isset($_SESSION " but dont know how to finish it correctly? " ?> 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. session_save_path("/home/content/q/1/w/q1w2e3r4t5y6u7/html/tmp/"); session_start(); I have this at the head of my index page. I'd like to count how many files are in temp to find out how many users are online. After going to my index page, i checked how many users were online via the script I wrote and it said 1. I refreshed the page and it said 2. It went as high as 10 with each refresh and then I stopped because something isn't right. I checked my temp folder and there are 10 temp files. How can one page visitor start so many unique sessions? Is there a way to prevent this? I'm using the session start command with dual purpose. One, if a user loads the page. Secondly, to check if $_SESSION['active'] exists so I know this is a registered user. How can i limit the amount of sessions started by an unknown user and efficiently count how many files are in the temp directory? i have two files in php both have the same code and the file is Code: [Select] <?php session_start(); $con = mysql_connect("l","root",""); // here localhost and password will be filled accordingly if (!$con) { die('Connection failu ' . mysql_error()); } mysql_select_db("student",$con); $fetch=mysql_query("SELECT * from student1") or die(mysql_error()); $row = mysql_fetch_array($fetch) or die(mysql_error()); sleep(10); echo "Name: ".$row[0]."</br>"; echo " Age: ".$row[1]."</br>"; echo " Address: ".$row[2]; mysql_close($con); ?> Now i have another file which has the same code as above except there is no sleep function used in it . Now when i run the file which is wothout sleep it displyas results in seconds however the file with sleep function takes it time. Now the problem is if i load the file woth sleep function first then its delayed nature is reflected in another file which is without sleep() i.e now the file without sleep is taking longer time to open. please explain all this and possible solution to this problem the other file is Code: [Select] <?php session_start(); $con = mysql_connect("localhost","root","instablogs"); if (!$con) { die('Connection failu ' . mysql_error()); } mysql_select_db("student",$con); $fetch=mysql_query("SELECT * from student1") or die(mysql_error()); $row = mysql_fetch_array($fetch) or die(mysql_error()); echo "Name: ".$row[0]."</br>"; echo " Age: ".$row[1]."</br>"; echo " Address: ".$row[2]."</br>"; mysql_close($con); ?> Im makigna simple login system for a sight but i only want them to have to login if they want to go to the shopping cart i have t implement a session_start() on every page before the cart page so the home page products page etc etc and then on the cart page ive a simple function that checks if their logged in if not then it will direct them to the login page if they are they should just continue is this the correct way to do it ? thanx |