Page 1 of 1
If statement problem
Posted: Fri Feb 19, 2010 12:20 pm
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!
Re: If statement problem
Posted: Fri Feb 19, 2010 12:33 pm
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?
Re: If statement problem
Posted: Fri Feb 19, 2010 1:10 pm
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!
Re: If statement problem
Posted: Fri Feb 19, 2010 1:32 pm
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;
}