Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-11-2008, 11:32 PM   PM User | #1
trixx
New Coder

 
Join Date: Sep 2008
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
trixx is an unknown quantity at this point
Exclamation Need Help !! PLZ

hey erm im a kinda newbie to php have been using it for about 6 months and have ended up with a problem . I have got a site which i use for school but as the school is hooked up on a domain sometimes the different users cookies info displays and messes up . The script i am using is below if any person could help me or make me a login script which uses sessions instead of cookies and i am also able to extract username from sessions to help me fetch data from database

PHP Code:
<?php
// Connects to your Database
mysql_connect("dbhost""dbuser""dbpass") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{
$username $_COOKIE['ID_my_site'];
$pass $_COOKIE['Key_my_site'];
$check mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
$total_regged=mysql_num_rows(mysql_query("SELECT * FROM users"));
while(
$info mysql_fetch_array$check ))
{
if (
$pass != $info['password'])
{
}
else
{
header("Location: feed.php");

}
}
}

//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted

// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die(
'You did not fill in a required field.');
}
// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 mysql_num_rows($check);
if (
$check2 == 0) {
die(
'That user does not exist in our database. <a href=reg.php>Click Here to Register</a>');
}
while(
$info mysql_fetch_array$check ))
{

//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die(
'Incorrect password, please try again.');
}
else
{

// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour time() + 3600;
setcookie(ID_my_site$_POST['username'], $hour);
setcookie(Key_my_site$_POST['pass'], $hour);

if (
strip_tags($_POST['submit'])){
$timenow=time();
mysql_query("UPDATE users SET online='$timenow' WHERE username='$username'");
mysql_query("UPDATE users SET ghostmode='$timenow' WHERE username='$username'");
}

if (
strip_tags($_POST['submit'])){
$last=$_SERVER['REMOTE_ADDR'];
mysql_query("UPDATE users SET last='$last' WHERE username='$username'");
}

//then redirect them to the members area
header("Location: feed.php");
}
}
}
else
{

// if they are not logged in
}
?>
this is what i am currently using on my pages to give you an idea on what im stuck on

PHP Code:
<?php
// Connects to your Database
mysql_connect("dbhost""dbuser""dbpass") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

if(isset(
$_COOKIE['ID_my_site']))
$username $_COOKIE['ID_my_site'];
$pass $_COOKIE['Key_my_site'];
$check mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while(
$info mysql_fetch_array$check ))
if (
$pass != $info['password'])
{(
"Location : index.php");
}
$fetch=mysql_fetch_object(mysql_query("SELECT * FROM users WHERE username='$username'"));
$metch=mysql_fetch_object(mysql_query("SELECT * FROM messages WHERE user_to='all'"));
$newpoints=$fetch->index2+1;
mysql_query("UPDATE visits SET index2='$newpoints' WHERE username='$username'");
?>
i need the $username= thing so i could extract the username and other info and echo it using sessions instead of cookies could anyone help me plz also how wud i go about including bbcode into my pages

thanks in advance
trixx is offline   Reply With Quote
Old 09-12-2008, 12:10 PM   PM User | #2
mic2100
Regular Coder

 
mic2100's Avatar
 
Join Date: Feb 2006
Location: Scunthorpe
Posts: 562
Thanks: 15
Thanked 28 Times in 27 Posts
mic2100 is on a distinguished road
there will be a few changes u need to do to achieve this

first you gotta take away all references to $_COOKIE and replace them with $_SESSION, then change...

PHP Code:
setcookie(ID_my_site$_POST['username'], $hour); 
setcookie(Key_my_site$_POST['pass'], $hour); 
to...
PHP Code:
$_SESSION['ID_my_site'] = $_POST['username']; 
$_SESSION['Key_my_site'] = $_POST['pass']; 
you must do this with any references to setcookie()

you will need to change all of these throughout the whole site for it to work correctly.
also i have found that session garbage collection or webservers can be done quite quickly some times, i wud recommend that you create a manual location to store sessions (outside of the root if possible), you do this like so...

PHP Code:
ini_set("session.save_path"$_SERVER['DOCUMENT_ROOT']."/sessions"); //sets the save path for the session variables 
ini_set("session.gc_maxlifetime""10000"); //sets the lifetime of the session before it removed.
session_start(); 
mic2100 is offline   Reply With Quote
Users who have thanked mic2100 for this post:
trixx (09-12-2008)
Old 09-12-2008, 03:15 PM   PM User | #3
BoltZ
New Coder

 
Join Date: Sep 2008
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
BoltZ is an unknown quantity at this point
Are you talking about a login script that logs the person out when they close the browser?
BoltZ is offline   Reply With Quote
Old 09-12-2008, 04:20 PM   PM User | #4
trixx
New Coder

 
Join Date: Sep 2008
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
trixx is an unknown quantity at this point
this is not working for me as the code i need is for me to use the session information to display user info

e.g.

PHP Code:
);
}
$fetch=mysql_fetch_object(mysql_query("SELECT * FROM users WHERE username='$username'")); 
This is the code i will use to fetch the data from that specified username

For the moment i am using cookies so my $username bit is this
PHP Code:
]))
$username $_COOKIE['ID_my_site']; 
i want to get rid of cookies and use sessions but still be able to do this

PHP Code:
$username get_info_from_session

echo $fetch->username 
something along these lines

Last edited by trixx; 09-12-2008 at 04:45 PM..
trixx is offline   Reply With Quote
Old 09-12-2008, 05:07 PM   PM User | #5
mic2100
Regular Coder

 
mic2100's Avatar
 
Join Date: Feb 2006
Location: Scunthorpe
Posts: 562
Thanks: 15
Thanked 28 Times in 27 Posts
mic2100 is on a distinguished road
to access the elements of the session u need to access them like this...
PHP Code:
$username $_SESSION['ID_my_site']; 

Last edited by mic2100; 09-12-2008 at 05:08 PM.. Reason: put [php] brackets in
mic2100 is offline   Reply With Quote
Old 09-12-2008, 05:15 PM   PM User | #6
trixx
New Coder

 
Join Date: Sep 2008
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
trixx is an unknown quantity at this point
k but would i have to lyk have a session start at the beginnin if i did how wud i go about doin that also hw wud i go about puttin bbcode in my pages

e.g if sum1 ryts sumfin in a text box which gets updated into their data hw wud i display it on their profile page

e.g

textbox [img]url[/img]

to be displayed on profile as image
trixx is offline   Reply With Quote
Old 09-12-2008, 05:19 PM   PM User | #7
mic2100
Regular Coder

 
mic2100's Avatar
 
Join Date: Feb 2006
Location: Scunthorpe
Posts: 562
Thanks: 15
Thanked 28 Times in 27 Posts
mic2100 is on a distinguished road
if you are wanting to do BB code i suggest downloading a BBcode converter

this is one i found on google but there are loads out there...
http://www.iceteks.com/articles.php/javascript2/3
mic2100 is offline   Reply With Quote
Old 09-12-2008, 05:21 PM   PM User | #8
trixx
New Coder

 
Join Date: Sep 2008
Posts: 94
Thanks: 4
Thanked 0 Times in 0 Posts
trixx is an unknown quantity at this point
k wot about my other problem
trixx is offline   Reply With Quote
Reply

Bookmarks

Tags
login problem, newbie, php, stuck

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:04 PM.


Advertisement
Log in to turn off these ads.