session_start

how do i know if im using the session data

Page 1 of 1

3 Replies - 1190 Views - Last Post: 15 November 2009 - 06:38 AM Rate Topic: -----

#1 chris_s_22   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 27-December 08

session_start

Posted 14 November 2009 - 09:19 AM

im trying to get to grips with using sessions

i have a page connection.php - this holds database details and connects to database and also uses session_start this page also does a php include to my functions.php page.

in my functions page in the login_user function i do
// Now encrypt the data to be stored in the session
	 $encrypted_id = md5($user['id']);
	 $encrypted_name = md5($user['username']);

	 // Store the data in the session
	 $_SESSION['id'] = $id;
	 $_SESSION['username'] = $username;
	 $_SESSION['encrypted_id'] = $encrypted_id;
	 $_SESSION['encrypted_name'] = $encrypted_name;


my questions are
on all other pages i create do i need to use session_start so the data is passed and ready for me to use? if not what do i do so i can use the session data to do checks.
do i still need to use session_start if on the page i do a php include to connection.php or would this be classed as doing the same thing since i do a session_start in my connection.php page.

last question if i have a form that send data to a page, that pages then checks data then calls for profile_edit function that i wanted to do a query to my database using something like this
<?php
function profile_edit($forminput1)
$query = ("UPDATE users SET feild1='$forminput1' WHERE username='$username'");
$result = mysql_query ($query) or die ('Could not edit user.');
	if($result)
	{	
	// Reshow the form with an error
	$updateeditprofile_error = 'error';
	include 'profileeditform.php';
	exit;
	}
	else
	{
echo "hello";
	}
}
?>


how do i do some kind of check to make sure my code in the query is using the session data $username

Is This A Good Question/Topic? 0
  • +

Replies To: session_start

#2 Xmod   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 19-January 07

Re: session_start

Posted 14 November 2009 - 10:25 AM

check like this

//check is session username is set
if(isset($_SESSION['username'])){
//do this
}
Was This Post Helpful? 0
  • +
  • -

#3 Xmod   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 19-January 07

Re: session_start

Posted 14 November 2009 - 10:37 AM

if I were you i would start sessions when users login

and also create an access.php with code like this

//if user is not logged in send them to login.php
if(!isset($_SESSION['username'])){
header("location: http://www.web.com/login.php");
}

and if they are just let then see the form... but also check the form when is submitted if user is logged in...

This post has been edited by Xmod: 14 November 2009 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 nick1200   User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: session_start

Posted 15 November 2009 - 06:38 AM

if you want to carry the id or password or what ever from page to page then ull allways have to use session_start
anything with a php script on use session_start like a register script or if you want to show the users info
the include function is just really to make the page short and less messy

so if we had a connection.php file
with a mysql conect in there
then we just do a include connection.php
on top of every page you want to connect to the db
if you have include connection.php then you only have to connect to the db 1ce

hope that helps :)
Was This Post Helpful? 1

Page 1 of 1