If statement problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
citrustech
Forum Newbie
Posts: 4
Joined: Sun Nov 22, 2009 6:14 pm

If statement problem

Post by citrustech »

How exactly would i do this?
If user logged in AND has not paid, redirect.
Else, echo "You're a paid member".


I mainly just need to outline on that.
Thanks a lot you guys!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: If statement problem

Post by AbraCadaver »

Well it depends. First you need to log them in and set something to designate that they are logged in. Second, how do you determine if they are a paid member? Is it in a field in a database?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
citrustech
Forum Newbie
Posts: 4
Joined: Sun Nov 22, 2009 6:14 pm

Re: If statement problem

Post by citrustech »

The whole login system and paid system is already finished.
Here's the script for checking if the user paid:

Code: Select all

 
<?
$con=mysql_connect("localhost", "XXX", "XXX") or die(mysql_error());
mysql_select_db("XXX") or die(mysql_error());
$result = mysql_query("SELECT username FROM users where username='"  . $username . "' and Paid='1'") or die(mysql_error());
$row = mysql_fetch_array($result);
echo $row['paid'];
$num_rows = mysql_num_rows($result);
if ($num_rows!=1){header("Location:http://XXX/paynow.php");}
mysql_close($con);
?>
 
And this chekcs to see if the user is logged in. If they are logged out, it will redirect them:

Code: Select all

 
<?php // If user not logged in, send back to login.if(empty($_SESSION['userName'])){header('location: login.php');}?><?php
session_start();
 
if(isset($_SESSION['username']))
{
$username=$_SESSION['username'];  
}
else
{
header('Location: user_login.php');
  print "Session does not exist"; 
}
?>
 
Now how would i combine that so it would check to see if the user is logged in? If YES, check to see if person paid. If person paid, don't do anything. If person didn't pay, redirect. - Thanks!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: If statement problem

Post by AbraCadaver »

I would refactor this in the code where you actually log them in. When you log them in, lookup if they are paid as well and set a session var for paid 0 or 1. Then to check you just do:

Code: Select all

if(isset($_SESSION['username']) && $_SESSION['paid'] == 0) {
    // redirect somewhere
}
What happens if they aren't logged in?

In addition, if a user is not paid and then they later pay, you would want to update the session var when you update the database.

You could probably help yourself and simplify a lot of your code and reduce repetition if you used some includes (for the db connection and stuff) and functions, for example:

Code: Select all

function is_loggedin() {
   if(isset($_SESSION['username'])) {
      return true;
   }
   return false;
}
 
function is_paid() {
   if(isset($_SESSION['paid']) && $_SESSION['paid'] == 1) {
      return true;
   }
   return false;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply