Mysql php checking paid field

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Mysql php checking paid field

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
}
?>
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post by ekosoftco »

That code isnt working for me either.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>';
?>
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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
)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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]
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post by ekosoftco »

That didnt seem to work
AshrakTheWhite
Forum Commoner
Posts: 69
Joined: Thu Feb 02, 2006 6:47 am

Post by AshrakTheWhite »

Code: Select all

INSERT INTO `TableName` (`COLname1`, `COLname2`) VALUES(``, `value`) WHERE `UserIDTable`.UserID = $userID
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
    }
}
?>
Post Reply