Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Sessions


PHP Sessions

Reply
  #11 (permalink)  
Old 07-07-09, 10:05 AM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
Very good points End User, I have to concede then that sessions would be better. Now that we have squashed my idea of the OP using cookies perhaps it would be a good idea to answer is original question as to how to use sessions.

Its something like this

<?php
session_start();
$_SESSION['My_Varable'] = "my_data";
echo $_SESSION['My_Varable'];
session_destroy();
?>

Basically you
1: Start the session
2. Assign valuables to the array, and assign values
3.Destroy the session when your done

Ill look around and see if I can find a tutorial for you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #12 (permalink)  
Old 07-07-09, 01:41 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by ruteckycs View Post
Very good points End User, I have to concede then that sessions would be better. Now that we have squashed my idea of the OP using cookies perhaps it would be a good idea to answer is original question as to how to use sessions.

Ill look around and see if I can find a tutorial for you.
Actually, I think my first post in this thread gave an example of using sessions.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #13 (permalink)  
Old 07-07-09, 07:04 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Quote:
Originally Posted by ruteckycs View Post
Sorry my friend I think you are confused. Sessions are NOT Cookies. Sessions are stored server side, Cookies are stored client side.

I say use cookies because as I recall for me , when I was first coding, cookies were much easier to understand and work with.

Being that a default php.ini says
Quote:
session.use_cookies specifies whether the module will use cookies to store the session id on the client side. Defaults to 1 (enabled).
And, most people will not disable that, then YES, sessions use cookies.

AT least on the 4,009,128 websites that I have visited in the past few years.

PS. 98% of my statistics are made up.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #14 (permalink)  
Old 07-07-09, 08:47 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
You didn't say sessions use cookies, you said they ARE cookies. They are not.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #15 (permalink)  
Old 07-08-09, 04:01 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
The only thing that sessions store in a cookie is the session identifier. It's a MD5 or SHA1 hash (depending on your php.ini settings). It contains no sensitive data which anyone could easily steal. The rest of the data is stored on the server, and it's only accessibly with the right session identifier.

Storing the username/password directly in a cookie, without sessions, would obviously be more insecure.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #16 (permalink)  
Old 07-08-09, 04:22 AM
klaniak klaniak is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Underground
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
Here is an example if you want to use sessions:
My guess is probably you want to use mysql
PHP Code:

//you need to connect to database


session_start(); //we start the session
if(isset($_POST['submit']))
{
 
$user=strip_tags(addslashes(($_POST['username']));
 
$password=strip_tags(addslashes($_POST['password']));
//you select your database
//you grab the user id and name and compare them with $user and $password

$_SESSION['valid_user']="your id number from database";
$_SESSION['name']="the name of the user";

header('location: menu.php'); //the location where you want your users to be redirected
} else {
  
header('location: sorry.php?not_logged=yes'); //the location if they are not logged
 

You can use the code and include it on all your protected pages,
include('./library/login.php');
if(!$_SESSION['valid_user']){header('location: sorry.php');}

You will need also a logout.php to destroy your session, here is an example:

PHP Code:

session_start();

unset(
$_SESSION);
session_destroy();
header('location: index.php'); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
how to solve this PHP error? j14nhAo PHP 1 02-16-06 08:48 AM
setting PHP sessions in flash phizzlecom PHP 1 11-08-04 09:20 PM
Getting PHP to use cookies for sessions perleo PHP 1 10-24-04 08:56 PM
PHP & sessions, why won't it work? TinnyFusion PHP 1 10-04-03 02:51 PM


All times are GMT -5. The time now is 09:10 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.