Jump to content

[SOLVED] my Session functions


MasterACE14

Recommended Posts

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);


?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Excellant! no error messages now  :) , Dragen saves the day again  ;D , 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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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'] . '" />';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :D

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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:8) 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();
   
    }
};
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.