Jump to content

Sessions / Cookies


markspec87

Recommended Posts

i thought it best to make a thread about this.

Ive got a signup / login script sorted now (adds and checks info in databse)

But im unsure how to use cookies or sessions to implement into it my site.

What code or commands do i need to use to check if someones logged in and/or log them in / out?

thanks
Link to comment
Share on other sites

So would this be right?

[quote]session_start(  );

    session_register("username");
    session_register("accesslevel");

    $username=$_POST['nickname'];
    $accesslevel = "select accesslevel from members where username = '".$_POST['username']."' and password = '".$_POST['password']."';";

[/quote]

Thats part of the code if they have specified the right username and password.

Guidance would be appreciated :)
Link to comment
Share on other sites

Nope. Should be something like this:
[code=php:0]<?php
//start the session
session_start();

// connect to database first
@mysql_connect('localhost', 'db_user', 'db_user_pass') or die("Unable to connect to MySQL at this time");

@mysql_select_db('db_name') or die("Unable to select database");

// make the input safe:
$user = mysql_real_escape_string($_POST['username']);

// you should encrypt users password. rather than using plain text passwords.
$pass = mysql_real_escape_string($_POST['password']);

// perform the query first
$sql = "SELECT accesslevel FROM members WHERE username='".$user."' AND `password`=".$pass."';";

$result = @mysql_query($sql) or die("Unable to perform query");

// check that 1 row was returned, meaning a match is found
if(mysql_num_rows($result) == 1)
{
    // retrieve the access level
    $row = mysql_fetch_assoc($result);
   
    // set the session vars:
    $_SESSION['username'] = $_POST['nickname'];
    $_SESSION['accesslevel'] = $row['accessleve;'];
}[/code]
Link to comment
Share on other sites

You havnt started the sessions thats why. When ever you need to use session variables you need have session_start(); before you use any sessions, otherwise the sessions wont work.
[code=php:0]<?php
// MUST START THE SESSION FIRST
// BEFORE USING ANY SESSION VARIABLES
session_start();

if (isset($_SESSION['username']))
{
    echo 'Welcome, ' . $_SESSION['username'];
}
else
{
    echo "Welcome to my website!";

}
?>[/code]
Link to comment
Share on other sites

Some success

The code works but it also thorws errors above the working text:

[quote]Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/terraarm/public_html/index.php:6) in /home/terraarm/public_html/index.php on line 30[/quote]

Any fix for this except covering the entirity of my page in php tags and echoing?

EDIT: Never mind fixed that :)
Link to comment
Share on other sites

It is important that you don't have any kind of output before session_start(); - except for coments. A space between php open and session_start(); will killl you.

correct:

[code]
<?
session_start();

session_destroy();
?>[/code]

Incorrect:
[code]
<?

session_start();

session_destroy();
?>[/code]
Link to comment
Share on other sites

[quote author=quillspirit link=topic=107001.msg429344#msg429344 date=1157561387]
It is important that you don't have any kind of output before session_start(); - except for coments. A space between php open and session_start(); will killl you.

correct:

[code]
<?
session_start();

session_destroy();
?>[/code]

Incorrect:
[code]
<?

session_start();

session_destroy();
?>[/code]
[/quote]
Thats is not a valid example as this will work
[code]
<?

session_start();

session_destroy();
?>[/code]
It wont work if there is output before you use session_start. Any whitspace between <?(php) and ?> will not be outputed and so you can put as much whitespace as you like within those tags. The only time when php will display the headers already sent error is when there is some form of outpu, eg whitespace, text/html before your use any header realted functions.
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.