MasterACE14 Posted September 25, 2007 Share Posted September 25, 2007 Evening Everyone, I have been working on making my own functions for handling Sessions, And I haven't tested it just yet, and I have only just started learning about Sessions and Cookies. So could someone tell me if Im on the right track or not? Currently I have 2 functions, 1 to log the person in, and put their ID into a variable, which is then put into a Session variable. And the other function checks whether a session exists(whether the person is logged in or not). here's what I got: <?php // conflicting forces functions //################ Session related functions ########################## function player_login($_POST["username"]) { session_start(); $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for selecting the logged in players id $sql = "SELECT `id` FROM `cf_users` WHERE `username`=".$_POST["username"].""; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // Put id in a variable for the session $player_id = $sql; // Put player id variable into session variable $_SESSION['playerid'] = $player_id; }; function player_session() { session_start(); // Use $HTTP_SESSION_VARS with PHP 4.0.6 or less if (!isset($_SESSION['playerid'])) { header("Location: http://www.crikeygames.com.au/conflictingforces/index.php?page=home"); } else { $_SESSION['playerid'] = $player_id; } }; ?> Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/ Share on other sites More sharing options...
MasterACE14 Posted September 25, 2007 Author Share Posted September 25, 2007 Just added logout function: <?php function player_logout() { session_start(); $_SESSION['playerid'] = $player_id; session_destroy(); }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354788 Share on other sites More sharing options...
MasterACE14 Posted September 25, 2007 Author Share Posted September 25, 2007 This is my latesst script, I fixed a minor bug with $_POST["username"] being used as a function variable: <?php // conflicting forces functions //################ Session related functions ########################## function player_login($player_id) { session_start(); $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for selecting the logged in players id $sql = "SELECT `id` FROM `cf_users` WHERE `username`='$player_id'"; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // Put id in a variable for the session $player_id = $sql; // Put player id variable into session variable $_SESSION['playerid'] = $player_id; header("Location: http://www.crikeygames.com.au/conflictingforces/index.php?page=base"); }; function player_session() { session_start(); // Use $HTTP_SESSION_VARS with PHP 4.0.6 or less if (!isset($_SESSION['playerid'])) { header("Location: http://www.crikeygames.com.au/conflictingforces/index.php?page=home"); } else { $_SESSION['playerid'] = $player_id; } }; function player_logout() { session_start(); $_SESSION['playerid'] = $player_id; session_destroy(); }; ?> here's my current login page: <?php // login the user /* includes */ include '/home/ace/public_html/conflictingforces/functions.php'; $player_id = $_POST["username"]; player_login($player_id); ?> when I log in, I receive the following errors: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/ace/public_html/conflictingforces/index.php:5) in /home/ace/public_html/conflictingforces/functions.php on line 6 Warning: Cannot modify header information - headers already sent by (output started at /home/ace/public_html/conflictingforces/index.php:5) in /home/ace/public_html/conflictingforces/functions.php on line 21 Now I've looked through google, and it says these errors are caused by whitespaces, or HTML code before the start_session function, in my case their is white space, but how can I fix that when I'm making a function? and the other error is caused by using the header() function. So how can I make these functions, and work around these problems? I need the login page to automatically redirect to my base.php page, and I need the session_start and all the sessions stuff in my own custom session functions. Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354860 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Share Posted September 25, 2007 session_start(); must be put at the top of every page, before any other code. You need to go through your code and remove it from within your functions etc and just place it at the top right below the <?php tag. Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354862 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 Also take a look at the output buffering functions. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354863 Share on other sites More sharing options...
MasterACE14 Posted September 25, 2007 Author Share Posted September 25, 2007 Im now getting this error: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/ace/public_html/conflictingforces/index.php:5) in /home/ace/public_html/conflictingforces/lib/login.php on line 1 login.php: <?php session_start(); // login the user /* includes */ include '/home/ace/public_html/conflictingforces/functions.php'; $player_id = $_POST["username"]; player_login($player_id); ?> Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354866 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Share Posted September 25, 2007 is login.php included in index.php? if it is you shouldn't need the session_start in login.php. You only need it for each file which stands by itself. If it is included within another file which already contains session_start, then another session_start is not needed. Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354870 Share on other sites More sharing options...
MasterACE14 Posted September 25, 2007 Author Share Posted September 25, 2007 nope its not, I have my index.php page set out in CSS with a left nav, right nav, header, footer, and the center, the center is where each page is displayed. using this switch statement: <?php switch ($_GET['page']) { default: case "home": require_once ("lib/home.php"); break; case "progress": require_once ("lib/progress.php"); break; case "support": require_once ("lib/support.php"); break; case "encyclopedia": require_once ("lib/encyclopedia.php"); break; case "encyclopedia_search": require_once ("lib/encyclopedia_search.php"); break; case "base": require_once ("lib/base.php"); break; case "login": require_once ("lib/login.php"); break; }// end switch ?> I haven't got session_start() anywhere but those files in the switch statement. Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354871 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Share Posted September 25, 2007 that's the problem then. You're outputting php (the switch) before session start. at the top of index.php put this: <?php session_start(); Then remove it from the other files. Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354875 Share on other sites More sharing options...
MasterACE14 Posted September 25, 2007 Author Share Posted September 25, 2007 Excellant! no error messages now , Dragen saves the day again , one question though, with the session_start() function at the top of index.php which is a page that 'technically' people dont have to log into, the session function isnt going to force them to log in or anything is it? or does it just create a session, and destroy it upon them closing their browser or leaving? Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354882 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 One question, with session_start() at the top of index.php it isnt going to force them to log in or anything is it? Nope Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354883 Share on other sites More sharing options...
MasterACE14 Posted September 25, 2007 Author Share Posted September 25, 2007 ok, thanks huggiebear I got 1 more question lol(sorry for dragging it out :-\) Im getting error messages when using header() now, does this mean I cant use header on any pages now becauses of the session_start() ? or do I have to set cookies and stuff in HTML now? Will I have to use javascript to redirect to another page? or is their a way to get the header() function working again? Could I possibly make a function that could replace the header() function? one that wont interfere with session_start() ? or can I use some of the functions that were in the link you gave me huggie? http://uk.php.net/manual/en/ref.outcontrol.php could I possibly do: ob_start(); at the very beginning of index.php and do: ob_end_flush(); at the very end of index.php which would then apply to every page, to allow me to use the header function? or would I be better off making my own header() function with the ob functions at the beginning and end of it? Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354886 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 The functions I provided you with are designed for stuff like that and you shouldn't need any client side header setting. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354897 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Share Posted September 25, 2007 You could try a simple meta refresh if needs be.. instead of: header("Location: http://www.crikeygames.com.au/conflictingforces/index.php?page=home"); use something like: echo '<meta http-equiv="refresh" content="0;url=' . $_SERVER['REQUEST_URI'] . '" />'; Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354899 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 You could try a simple meta refresh if needs be.. instead of: header("Location: http://www.crikeygames.com.au/conflictingforces/index.php?page=home"); use something like: echo '<meta http-equiv="refresh" content="0;url=' . $_SERVER['REQUEST_URI'] . '" />'; Horrible way to do things... It's a workaround for crap coding in the first place Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354903 Share on other sites More sharing options...
MasterACE14 Posted September 25, 2007 Author Share Posted September 25, 2007 yeah I just tried the meta way, and the page goes into a refreshing frenzy and doesn't change to the correct page at all :-\ , I'm still alittle lost with all the output control functions. would I do it the first way I said, at beginning of index.php and at the end? or would I just do it before and after the header() function? could you give me an example? I'm still learning this session stuff Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354906 Share on other sites More sharing options...
Dragen Posted September 25, 2007 Share Posted September 25, 2007 yeah I know.. that's why I said 'if needs be' Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354907 Share on other sites More sharing options...
MasterACE14 Posted September 25, 2007 Author Share Posted September 25, 2007 (wish they had a better smiley lol) I got to hit the sack, If you think of anything knew, or could give me an example of output control, please do post it, I'll be back online tommorow afternoon, and this is the first post i'm checking Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354909 Share on other sites More sharing options...
HuggieBear Posted September 25, 2007 Share Posted September 25, 2007 There's a perfect example on the session_start() manual page... <?php // Function to see if the sessions started function session_started(){ if (isset($_SESSION)) { return true; } else { return false; } } // Start the output buffer so we dont create any errors with headers ob_start(); // Run the function to check to see if the session has been started if (session_started()) { echo 'The session has been started.<br />'; } else { // Without buffering, this would cause an error later when trying to start a session echo 'The session has not been started.<br />'; } //Start the session echo 'Starting Session...<br />'; // This would also cause an error session_start(); //Check again if (session_started()) { echo 'The session has been started.<br />'; } else { echo 'The session has not been started.<br />'; } //Flush the buffer to screen ob_end_flush(); ?> Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-354914 Share on other sites More sharing options...
MasterACE14 Posted September 26, 2007 Author Share Posted September 26, 2007 I got a problem with my logout function: <?php function player_logout() { $_SESSION['playerid'] = $player_id; session_destroy(); // Start the output buffer so we dont create any errors with headers ob_start(); header("Location: http://www.crikeygames.com.au/conflictingforces/index.php?page=home"); //Flush the buffer to screen ob_end_flush(); }; ?> here's the error I get: Warning: Cannot modify header information - headers already sent by (output started at /home/ace/public_html/conflictingforces/index.php: in /home/ace/public_html/conflictingforces/functions.php on line 76 Line 76 is the header function. The function is destroying the session fine, but its not redirecting. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ EDIT!: I've gotten around that problem, just made it print "you have successfully logged out" which I think is a better idea anyway. But I have the same problem with the header() function not working in my login function: <?php function player_login($player_id) { $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for selecting the logged in players id $sql = "SELECT `id` FROM `cf_users` WHERE `username`='$player_id'"; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // Put id in a variable for the session $player_id = $sql; // Put player id variable into session variable $_SESSION['playerid'] = $player_id; if (isset($_SESSION['playerid'])) { return true; // Start the output buffer so we dont create any errors with headers ob_start(); // send the person to the main page header("Location: http://www.crikeygames.com.au/conflictingforces/index.php?page=base"); //Flush the buffer to screen ob_end_flush(); } else { return false; // Start the output buffer so we dont create any errors with headers ob_start(); // send the person to the home page header("Location: http://www.crikeygames.com.au/conflictingforces/index.php?page=home"); //Flush the buffer to screen ob_end_flush(); } }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-355509 Share on other sites More sharing options...
MasterACE14 Posted September 26, 2007 Author Share Posted September 26, 2007 I also made a function to select different fields from the persons account in the database. But its displaying the query not the result of the query: <?php function player_table($select) { include "/home/ace/public_html/conflictingforces/sessionvars.php"; $where = $_SESSION['playerid']; $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "SELECT '$select' FROM `cf_users` WHERE `id`='$where'"; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); echo $sql; }; ?> and I have this on the main page: <?php player_table("username"); ?> and it is displaying this: SELECT 'username' FROM `cf_users` WHERE `id`='' instead of the username, which in my case should be ACE. Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-355521 Share on other sites More sharing options...
HuggieBear Posted September 26, 2007 Share Posted September 26, 2007 I also made a function to select different fields from the persons account in the database. But its displaying the query not the result of the query: That will be due to these lines... $sql = "SELECT '$select' FROM `cf_users` WHERE `id`='$where'"; echo $sql; Notice anything wrong Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-355529 Share on other sites More sharing options...
MasterACE14 Posted September 26, 2007 Author Share Posted September 26, 2007 im as blind as a bat :-\, what have I done wrong lol Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-355530 Share on other sites More sharing options...
HuggieBear Posted September 26, 2007 Share Posted September 26, 2007 You're seeing the sql, as that's what you're echoing. If you want the results displayed then you need to echo them... <?php // SQL query for all entries in descending order (Added a limit clause here as I assume you're only expecting 1 result) $sql = "SELECT '$select' FROM `cf_users` WHERE `id`='$where' LIMIT 1"; // Return a result resource (this still isn't in a form you can echo) $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // This is missing, this gets the actual data from the result resource $username = mysql_result($rs,0,0); /* This is changed echo $sql; to this */ echo $username; ?> Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-355536 Share on other sites More sharing options...
MasterACE14 Posted September 26, 2007 Author Share Posted September 26, 2007 dont know how I missed that one :-\ lol. I'm now getting this error though: Query: SELECT 'username' FROM `cf_users` WHERE `id`='SELECT `id` FROM `cf_users` WHERE `username`='ACE'' LIMIT 1 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ACE'' LIMIT 1' at line 1 I dont know how the session variable has the value of a MySQL query ??? login.php: <?php // login the user /* includes */ include "/home/ace/public_html/conflictingforces/functions.php"; $player_id = $_POST["username"]; player_login($player_id); ?> Quote Link to comment https://forums.phpfreaks.com/topic/70600-solved-my-session-functions/#findComment-355540 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.