Jump to content

mayutaka8985

New Members
  • Posts

    7
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

mayutaka8985's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I removed it and now it all works perfectly! Thank you so much!! I dunno why I had written that in there in the first place Thank you!
  2. I checked the id in the database and it's a primary key. I've tried closing and reopening my browser, deleting all cookies etc but that doesn't make a difference. I also searched through all my files and none have any other session data in them, just the usual $_SESSION['sessionname'] = 'content of var'; And any created session with $_SESSION on the index page works fine and carries across to other pages, but anything created on the other pages will not carry over...until I login and then suddenly all session variables work across pages and the new variables' data gets appended into the data field in the database.
  3. It's a test site on my local server so the url for index is: http://localhost:8888/i/index and the url for login is http://localhost:8888/i/login I don't actually need the session_set_save_handler, it's more of a learning experience so I know how to use it should I ever need it. I will try the things you suggested and see if I can pinpoint the issue. Thanks for your suggestions.
  4. I'm having an issue with sessions and session_set_save_handler. Note: new at OO PHP I'm using an MVC written mostly from a tutorial and it all seems to be working, except for sessions. I've got session_set_save_handler as a class SessionHandler. If I include it into the main index page and create an instance, I get an error 'Undefined variable: _SESSION' for my index view file. If I instead include a global.php file into the main index page and include the session class into the global.php page, and create an instance, the error goes away and session_set_save_handler saves the session variables into my database. (1)On my index page for debugging I've got a var_dump of $_SESSION to see which session variables are set. On my login page I've got a form token that is saved to a session variable. When I go to the login page that session token variable is set and doing a var_dump on the page shows it is indeed being set, however, when I click back to the main index page, the index var_dump(1) shows the session variable is no longer set. But then if I login it correctly redirects me to a user logged in page and echos the username of the user logged in correctly, but still does not show the form token session variable in the var_dump for index(1). But then if I navigate to the login form page where the form token should be initially set, it now sets it correctly and going to the index page indeed shows the token set. form token being set on login view page: $login_token = $_SESSION['login_token'] = md5(uniqid(mt_rand(),true)); var_dump on index page before login (even if I navigate to login page then back to index it remains blank when it should instead show the form token session variable) array empty var_dump on index page after login. array 'lu_user' => string 'admin' (length=5) 'lu_user_id' => string '6' (length=1) 'user_sess_time' => int 1326544148 var_dump on index page after login, navigating to login form page (where login_token is set), then navigating back to index: array 'lu_user' => string 'admin' (length=5) 'lu_user_id' => string '6' (length=1) 'user_sess_time' => int 1326544148 'login_token' => string 'c260e76dd65f0d9b6e881cfc9a4b33e1' (length=32) Magically the login_token now shows, but only after logging in and setting the other variables. It seems it's not saving the login_token when it's initially set, but only after logging in which it shouldn't be doing. The same issue arises with any other session variables that I set elsewhere (only displayed after user login). If I remove session_set_handler the sessions set correctly but naturally the data is no longer added to the database. If I do var_dump(get_included_files()); it shows the session class is correctly being included. I was thinking it's an issue with session_start() but if I add that in or try to create another instance of the session class on the other pages, I get an error stating they've already been called and cannot be called again. So then they must be getting called, right? So now I'm thoroughly confused. More code below, sorry for the long post. Hopefully I've provided all necessary information. Index includes global.php, which contains: require 'framework/SessionHandler.php'; $sess = new SessionHandler(); And the session handler class is (Database class is included into index which is why you cannot see it instantiated here): <?php class SessionHandler { function __construct() { session_set_save_handler ( array(&$this, 'open'), array(&$this, 'close'), array(&$this, 'read'), array(&$this, 'write'), array(&$this, 'destroy'), array(&$this, 'gc') ); session_start(); ini_set('session.gc-maxlifetime', 1800); if (isset($_SESSION['lu_user'])) { if (!isset($_SESSION['user_sess_time'])) { $_SESSION['user_sess_time'] = time(); } elseif (time() - $_SESSION['user_sess_time'] > 1800) // 30 mins { session_regenerate_id(TRUE); $_SESSION['user_sess_time'] = time(); } } } function open() { $this->db = new Database(); } function close() { return $this->db = null; } function read($id) { $stmt = $this->db->prepare('SELECT * FROM sessions WHERE id = :id'); $stmt->execute(array(':id' => $id)); if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { return $data = $row['data']; } else { return ''; } } function write($id, $data) { $access = time(); if (isset($_SESSION['lu_user'])) { $session_id = session_id(); $username = $_SESSION['lu_user']; $stmt = $this->db->prepare('REPLACE INTO sessions (id, access, data) VALUES (:id, :access, :data)'); $stmt->execute(array(':id' => $id, ':access' => $access, ':data' => $data)); } } function destroy($id) { if (ini_get("session.use_cookies")) { $params = session_get_cookie_params(); setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]); } $stmt = $this->db->prepare('DELETE FROM sessions WHERE id = :id'); $stmt->execute(array(':id' => $id)); } function gc($max) { $old = time() - $max; $stmt = $this->db->prepare('DELETE FROM sessions WHERE access < :old'); $stmt->execute(array(':old' => $old)); } function __destruct() { session_write_close(); } }
  5. Forgive me if this is the wrong area to post, I wasn't 100% sure. I'm trying to design a website for a TV show database where people can sign up as a member and then track the shows they are watching and keep track of which episodes they've seen. How it would work: Each TV show has it's own page on which there is information displayed about the show and then a listing of the episodes in a table with two columns: one containing the episode number and the other showing the status as either watched or not watched. I want to be able to keep track in a database which episodes for which show each user has watched/not watched, so each time they visit the site they can easily view which episode they are up to for which show. I hope that makes sense. The question is: how do I go about doing this (in regards to the episode listing and the episode's status)? No matter which idea I come up with for the database it leads to a seemingly ridiculous conclusion. Can someone please point me in the right direction of how I should go about this? Originally I thought something like: episodeListing table user_id || tv_id || ep_num || status 45434 || 234 || 3 || watched as columns in a database for the episode listing, but doing this would mean that there would be a separate row for each episode of each tv show for each user, meaning if 100 users watched a tv show with, for example, 16 episodes then it would be 1,600 entries just for the one tv show, which forgive me if I'm wrong, sounds a bit excessive and I'm sure it's a bad way to go about it. Ideas, please?
  6. Okay I'm a newbie at PHP but was trying to write a counter for my website to count the amount of times a specific button is clicked. My desired outcome is as follows: If the form submit button "like" or "dislike" is clicked, get user's ip and check to see if it already exists in the database. If it exists, don't increment the count (of either button), otherwise log the ip and increment the count (of whichever button is clicked) by 1. Currently I've got the increment working (testing only the like button at the moment) and I've got the ip being stored into the database, but cannot get the comparison to work. Each time the button is clicked it adds the ip address into the database instead of disallowing it. Note: there are two different tables--one storing the ip (counter) and one storing the like clicks (Designs) (dunno if there's a better way?) Excuse my newbieness. The problem area: (like button only): if(isset($_POST['like'])) { $ip_address = $_SERVER['REMOTE_ADDR']; $query = mysql_query("SELECT ip FROM counter WHERE ip = '$ip_address'"); $num_rows = mysql_num_rows($query); if ($num_rows > 0) { die ("You've already clicked the button!"); } else { mysql_query("INSERT INTO counter VALUES ($ip_address)"); mysql_query("UPDATE Designs SET up = up + 1 WHERE ref = 'cataclysm'"); } } $select = mysql_query("SELECT up FROM Designs"); while($row = mysql_fetch_array($select)) { echo $row['up']; }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.