rdkd1970 Posted June 20, 2011 Share Posted June 20, 2011 Can you use $_SESSION function differently in another page and it will pick up the other registered variable if it has not been used before. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/ Share on other sites More sharing options...
AbraCadaver Posted June 20, 2011 Share Posted June 20, 2011 Can you use $_SESSION function differently in another page and it will pick up the other registered variable if it has not been used before. What!?!? Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232326 Share on other sites More sharing options...
fugix Posted June 20, 2011 Share Posted June 20, 2011 Can you use $_SESSION function differently in another page and it will pick up the other registered variable if it has not been used before. $_SESSION is a predefined superglobal array, not a function. Also, I don't quite understand what it is that you are trying to ask here. Please be more clear with your question Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232330 Share on other sites More sharing options...
rdkd1970 Posted June 20, 2011 Author Share Posted June 20, 2011 if (!isset($_SESSION['SESS_ID']) || (trim($_SESSION['SESS_ID']) == '')) { Â '<a href="http://www.blessedtalk.com">Register Account</a> Â Â | Â Â <a href="http://www.blessedtalk.com/login-form.php">Log In</a>'; Â } $id = ""; $username = ""; $firstname = ""; $lastname = ""; // ------- ESTABLISH THE PAGE ID ACCORDING TO CONDITIONS --------- if (isset($_GET['id'])) { $id = $_GET['id']; // filter everything but numbers } else if (isset($_SESSION['SESS_ID'])) { $id = $_SESSION['SESS_ID']; } else { Â '<a href="http://www.blessedtalk.com/login-form.php">Log In</a>'; Â } if (!isset($_POST['post_type']) || !isset($_POST['post_body']) || !isset($_POST['fsID']) || !isset($_POST['fsTitle']) || !isset($_POST['uid']) || !isset($_POST['upass'])) { echo "Important variables from the form are missing."; exit(); } // Filter all of the common variables $post_type = $_POST['post_type']; $post_body = $_POST['post_body']; $post_body = nl2br(htmlspecialchars($post_body)); $post_body = mysql_real_escape_string($post_body); $forum_section_id = preg_replace('#[^0-9]#i', '', $_POST['fsID']); $forum_section_title = preg_replace('#[^A-Za-z 0-9]#i', '', $_POST['fsTitle']); $member_id = preg_replace('#[^0-9]#i', '', $_POST['uid']); $username =Â preg_replace('#[^0-9]#i', '', $_GET['username']); $member_password = mysql_real_escape_string($_POST['upass']); // Check the database to be sure that their ID, password, and email session variables all match in the database $u_id = $member_id; $sql = mysql_query("SELECT * FROM myMembers WHERE id='$u_id'"); $numRows = mysql_num_rows($sql); if ($numRows < 1) { Â Â echo "ERROR: You do not exist in the system"; Â Â exit(); } // Check the database to be sure that this forum section exists $sql = mysql_query("SELECT * FROM forum_sections WHERE id='$forum_section_id' AND title='$forum_section_title'"); $numRows = mysql_num_rows($sql); if ($numRows < 1) { Â Â echo "ERROR: That forum section does not exist."; Â Â exit(); } // Prevent this member from posting more than 30 times in one day $sql = mysql_query("SELECT id FROM forum_posts WHERE post_author_id='$member_id' AND DATE(date_time) = DATE(NOW()) LIMIT 32"); $numRows = mysql_num_rows($sql); if ($numRows > 30) { echo "ERROR: You can post only 30 times per day. Your maximum has been reached."; Â Â exit(); } // Add this post to the database now. The query depends on the "post_type" value // Only if the post_type is "a" /////////////////////////////////////////////////////////////////////////////////// if ($post_type == "a") { $post_title = preg_replace('#[^A-za-z0-9 ?!.,]#i', '', $_POST['post_title']); if ($post_title == "") { echo "The Topic Title is missing."; exit(); } if (strlen($post_title) < 10) { echo "Your Topic Title is less than 10 characters."; exit(); } $sql = mysql_query("INSERT INTO forum_posts (username, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) Â Â VALUES('".$username."','".$member_id."',now(),'a','".$forum_section_title."','".$forum_section_id."','".$post_title."','".$post_body."')") or die (mysql_error()); $this_id = mysql_insert_id(); //$sql = mysql_query("UPDATE forum_posts SET otid='$this_id' WHERE id='$this_id'"); header("location: view_thread.php?id=$this_id"); Â Â exit(); } Â I am trying to add a forum to this site. So I am grabbing the username from the main file which was set under the html folder and have a forum folder. All my pages have the above isset settings but this one page I need to add to another table the username. It grabs the id from the session but not the username. It worked twice and stopped working. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232338 Share on other sites More sharing options...
rdkd1970 Posted June 20, 2011 Author Share Posted June 20, 2011 The line that shows when I use the error codes is which is now $_GET as I have tried it all with no success. Â $username = $_SESSION[username] Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232341 Share on other sites More sharing options...
fugix Posted June 20, 2011 Share Posted June 20, 2011 So you are trying to grab the username from either a form or a query string in this line $username =  preg_replace('#[^0-9]#i', '', $_GET['username']); But it is not correctly grabbing the data? Do I understand you correctly Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232348 Share on other sites More sharing options...
mikesta707 Posted June 20, 2011 Share Posted June 20, 2011 Well first thing first, you have to use session_start() when using sessions on a page: http://php.net/manual/en/function.session-start.php  I also suggest you read a bit on how session variables work   Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232351 Share on other sites More sharing options...
rdkd1970 Posted June 20, 2011 Author Share Posted June 20, 2011 that is correct and I am using session_start(); on all my pages Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232359 Share on other sites More sharing options...
rdkd1970 Posted June 20, 2011 Author Share Posted June 20, 2011 More clearly I am trying to use that line which is not grabbing the username I actually have it now as $_SESSION[username] Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232361 Share on other sites More sharing options...
fugix Posted June 20, 2011 Share Posted June 20, 2011 Have you tried a print_r($_SESSION) to see what is stored as a session. Also, where do you actually set your $_SESSION['username'] Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232364 Share on other sites More sharing options...
rdkd1970 Posted June 20, 2011 Author Share Posted June 20, 2011 Yes I have tried the print_r and it produced nothing I had the !isset($_SESSION[username]) the isset($_SESSION[username]) nothing is grabbing that username. the table is blank all the other fields are being posted but that one. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232366 Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2011 Share Posted June 20, 2011 Do you have error_reporting and display_errors set to appropriate values for development? Does session_start exist in that script somewhere before any output is sent to the browser and before attempting to set/use any $SESSION vars? If not, it needs to . . . Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232369 Share on other sites More sharing options...
rdkd1970 Posted June 20, 2011 Author Share Posted June 20, 2011 All that is set according to what you mentioned that is how I know that it is not getting the variable at first I was not sure why but it pointed to that line in the script. The error reporting Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232370 Share on other sites More sharing options...
rdkd1970 Posted June 20, 2011 Author Share Posted June 20, 2011 I just re-entered the isset to this and it exited the page in testing. Â if (!isset($_SESSION['SESS_ID']) || (trim($_SESSION['SESS_ID']) == '') || !isset($_SESSION['username'])) { Â '<a href="http://index.php">Register Account</a> Â Â | Â Â <a href="login-form.php">Log In</a>'; exit(); Â } Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232373 Share on other sites More sharing options...
Pikachu2000 Posted June 20, 2011 Share Posted June 20, 2011 That code doesn't do anything but exit() if it hits that part of the conditional. There's no echo to send the html to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232383 Share on other sites More sharing options...
rdkd1970 Posted June 20, 2011 Author Share Posted June 20, 2011 I found out the string is empty but I am not sure how to fix it as the username is in the table with a value. I have two tables one is the main one members and the other I am trying to add to. I did a var_dump for the $_SESSION and all but of course the username was present. The username shows empty string. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232511 Share on other sites More sharing options...
fugix Posted June 21, 2011 Share Posted June 21, 2011 Where in your code do you set your $_SESSION['username'] Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232550 Share on other sites More sharing options...
rdkd1970 Posted June 21, 2011 Author Share Posted June 21, 2011 Here is my code I called my server company and had them add session_save_path I am not sure if that is the problem but it must be something easy. this is the parse.php file which willl run when the member has filled out the section in the forum they wish to post. Â Â php error_reporting(E_ALL); ini_set("display_errors", 1); Â session_start(); var_dump($_SESSION); Â // Connect to the database include_once "../Connection/mysql.php"; if (!isset($_SESSION['SESS_ID']) || (trim($_SESSION['SESS_ID']) == '') || !isset($_SESSION['username'])) { Â Â '<a href="index.php">Register Account</a> Â Â Â Â Â | Â Â Â Â Â <a href="login-form.php">Log In</a>'; Â Â Â Â } if (isset($_GET['id'])) { Â Â Â $id = $_GET['id']; // filter everything but numbers } else if (isset($_SESSION['SESS_ID'])) { Â Â Â $id = $_SESSION['SESS_ID']; } else { Â Â '<a href="login-form.php">Log In</a>'; Â Â } $sql = mysql_query("SELECT * FROM myMembers WHERE id='".$_SESSION['SESS_ID']."'"); $numRows = mysql_num_rows($sql); if ($numRows < 1) { Â Â Â Â echo "ERROR: You do not exist in the system"; Â Â Â Â exit(); } // Be sure all form variables are present to proceed if (!isset($_POST['post_type']) || !isset($_POST['post_body']) || !isset($_POST['fsID']) || !isset($_POST['fsTitle']) || !isset($_POST['uid']) || !isset($_POST['upass'])) { Â Â echo "Important variables from the form are missing."; Â Â exit(); } // Filter all of the common variables $post_type = $_POST['post_type']; Â $post_body = $_POST['post_body']; $post_body = nl2br(htmlspecialchars($post_body)); $post_body = mysql_real_escape_string($post_body); $forum_section_id = preg_replace('#[^0-9]#i', '', $_POST['fsID']); Â $forum_section_title = preg_replace('#[^A-Za-z 0-9]#i', '', $_POST['fsTitle']); Â $member_id = preg_replace('#[^0-9]#i', '', $_POST['uid']); Â $username =Â preg_replace('#[^0-9]#i', '', $_SESSION['username']); Â $member_password = mysql_real_escape_string($_POST['upass']); // Check the database to be sure that this forum section exists $sql = mysql_query("SELECT * FROM forum_sections WHERE id='$forum_section_id' AND title='$forum_section_title'"); $numRows = mysql_num_rows($sql); if ($numRows < 1) { Â Â Â Â echo "ERROR: That forum section does not exist."; Â Â Â Â exit(); } // Prevent this member from posting more than 30 times in one day $sql = mysql_query("SELECT id FROM forum_posts WHERE post_author_id='$member_id' AND DATE(date_time) = DATE(NOW()) LIMIT 32"); $numRows = mysql_num_rows($sql); if ($numRows > 30) { Â Â echo "ERROR: You can post only 30 times per day. Your maximum has been reached."; Â Â exit(); } // Add this post to the database now. The query depends on the "post_type" value // Only if the post_type is "a" ////////////////////// if ($post_type == "a") { Â Â $post_title = preg_replace('#[^A-za-z0-9 ?!.,]#i', '', $_POST['post_title']); Â Â Â Â if ($post_title == "") { echo "The Topic Title is missing."; exit(); } Â Â if (strlen($post_title) < 10) { echo "Your Topic Title is less than 10 characters."; exit(); } Â Â $sql = mysql_query("INSERT INTO forum_posts (username, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) Â Â Â VALUES('".$username."','".$member_id."',now(),'a','".$forum_section_title."','".$forum_section_id."','".$post_title."','".$post_body."')") or die (mysql_error()); Â Â $this_id = mysql_insert_id(); Â Â //$sql = mysql_query("UPDATE forum_posts SET otid='$this_id' WHERE id='$this_id'"); Â Â Â header("location: view_thread.php?id=$this_id"); Â Â Â exit(); }Â Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232822 Share on other sites More sharing options...
rdkd1970 Posted June 21, 2011 Author Share Posted June 21, 2011 I am now getting this message and it is not even showing the $_SESSION username.????? Â array(3) { ["SESS_ID"]=> string(1) "2" ["SESS_FIRST_NAME"]=> string(7) "Michael" ["SESS_LAST_NAME"]=> string(6) "Cooke" } Notice: Undefined index: username Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232828 Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 Apparently, $_SESSION['username'] is unset. You need to track down where the value is supposed to be assigned to $_SESSION['username'] and debug that. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232831 Share on other sites More sharing options...
Drummin Posted June 21, 2011 Share Posted June 21, 2011 You mentioned you have session start at the top of each page. You should only call this one time, probably on the login-form.php page where the user logs in. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232844 Share on other sites More sharing options...
rdkd1970 Posted June 21, 2011 Author Share Posted June 21, 2011 I am unsure what you are talking about do you mean check $_SESSION['username'] on all files from the beginning of registration and see where it is not responding. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232845 Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 You mentioned you have session start at the top of each page. You should only call this one time, probably on the login-form.php page where the user logs in.  session_start() needs to be called before any output is sent to the browser in each and every script that will make use of the $_SESSION array. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232857 Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 I am unsure what you are talking about do you mean check $_SESSION['username'] on all files from the beginning of registration and see where it is not responding. Â If that's what it takes to make sure it has a value assigned to it, and to figure out where that value is lost, then yes. Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232858 Share on other sites More sharing options...
rdkd1970 Posted June 21, 2011 Author Share Posted June 21, 2011 I put the var_dump $_SESSION on pages only shows up on the script I have submitted. I do have the main files under the html file but this one is a forum and I wanted people to view it that are not members like a forum is set up so it is under its own file forum when it goes to the server. I am not sure why it is losing the username session.???? Quote Link to comment https://forums.phpfreaks.com/topic/239906-using-sessions/#findComment-1232859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.