Page 1 of 2

Mysql php checking paid field

Posted: Fri Aug 04, 2006 8:25 pm
by ekosoftco
I have a site im making that will have users for a game, and i added a field called paid, so people who have paid for their account can access certain pages, i was just wondering how i could go about to code a page that lets only people who have the field value of Paid to Yes to view the contents.

Posted: Fri Aug 04, 2006 9:38 pm
by RobertGonzalez
When you build your login portion, set a session var that pulls that 'Paid' value from the users row. Use that as a check on each page. If, on each page, the session var 'Paid' is not Yes, redirect them to another section.

Posted: Fri Aug 04, 2006 9:53 pm
by ekosoftco
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php
ob_start();
//include some stuff
require("top.php");
if($_SESSION['Paid'] !== "Yes") {    

header("Location: paidonly.php");
exit;
}
?>
thats my code, and it refers everyone to paidonly.php (a page that lets non-paid users know that they cannot access the site).
I set my account to paid, and it keeps referring me.
just to know, the top.php is nothing more than accessing the database.


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Aug 04, 2006 9:58 pm
by RobertGonzalez

Code: Select all

<?php
/**
 * If you do not need output buffering don't use it
 * Replaced ob_start() with session_start()
 **/
session_start();
// include db abstractions
require("top.php");

if (!isset($_SESSION['Paid']) || $_SESSION['Paid'] != "Yes") {   
    header("Location: http://www.usefullurlswithheader.com/paidonly.php");
    exit;
}
?>

Posted: Fri Aug 04, 2006 10:03 pm
by ekosoftco
That code isnt working for me either.

Posted: Fri Aug 04, 2006 10:20 pm
by RobertGonzalez
OK, before you do any includes or redirects, run just this portion of code in its own page and post back the output.

Code: Select all

<?php
session_start();

echo '<h1>The session id is ' . session_id() . '</h1>';

echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>

Posted: Fri Aug 04, 2006 10:27 pm
by ekosoftco

Code: Select all

The session id is 83aa8b8a1355dcc183c5b15b6d1ae385Array
(
    [Uname] => 
    [lp] => 
    [admin_in] => 1
    [admin_id] => 1
    [admin_user] => Cyril
    [admin_name] => Grant Kessler
    [admin_lastlogin] => 1154609852
)

Posted: Fri Aug 04, 2006 10:43 pm
by RobertGonzalez
The problem is that you do not have a session var named 'Paid'. You need to make that var, then set it to the value in the database, then you can compare against it.

Posted: Fri Aug 04, 2006 10:57 pm
by ekosoftco
Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


So something like this in the query?

[syntax="sql"]INSERT INTO loginphp(
Paid
)
VALUES (
'`Paid`'
)
?


Everah | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Aug 04, 2006 11:08 pm
by ekosoftco
That didnt seem to work

Posted: Sat Aug 05, 2006 12:49 am
by AshrakTheWhite

Code: Select all

INSERT INTO `TableName` (`COLname1`, `COLname2`) VALUES(``, `value`) WHERE `UserIDTable`.UserID = $userID

Posted: Sat Aug 05, 2006 10:14 am
by ekosoftco
Is it possible i can get an explaination of this? im self taught, so i dont know some things and known exactly how to do others. i know nothing about sessions really. I have a field called Paid already, but the session wont work, if that is the correctly way to make it work, can someone possibly give me an explanation of what to do following that example? :/ sorry guys. Im trying to learn though

Posted: Sun Aug 06, 2006 9:49 am
by RobertGonzalez
Can you post a MySQL Table structure dump so we can see your fields and field types? You may need to add a field to your user table, then you will need to update that field and when pulling user data upon login, grab the paid column and make a session var out of it.

But before all that, post your table structure.

Posted: Sun Aug 06, 2006 9:56 am
by ekosoftco

Code: Select all

loginphp
Field Type Null Default 
id   int(11) No      
Uname   varchar(30) Yes  NULL    
Email   varchar(30) Yes  NULL    
Fname   varchar(30) Yes  NULL    
Lname   varchar(30) Yes  NULL    
Pword   varchar(30) Yes  NULL    
Kills   int(11) No      
Cname1   varchar(30) No      
Cname2   varchar(30) No      
Cname3   varchar(30) No      
Deaths   int(11) No      
Drate   int(11) No      
Paid   varchar(30) No      


 Indexes: Keyname Type Cardinality Field 
PRIMARY  PRIMARY 6   id

Posted: Sun Aug 06, 2006 2:49 pm
by RobertGonzalez
I would maybe make paid a tinyint (1) field and have it be either a 1 or 0. Unpaid would be 0, paid would be 1. When you grab the users information upon login and set your session vars, add a session var called 'user_paid' to that list. Then, on each page, check it like this...

Code: Select all

<?php
session_start();

if (isset($_SESSION['user_paid']))
{
    if ($_SESSION['user_paid'])
    {
        // Let me in!
    }
    else
    {
        header("Location: http://www.takemesomeplaceelse.com/");
        exit;
    }
}
?>