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!
I am trying to do some username flagging on a page, where even if the user has signed up, they still need to be approved to view content. This is the code that I have, but it seems not to work correctly. Would anyone have some suggestions?
<?php
$query = "SELECT approved,username FROM members where username="$username";
$queryapproved = $query['approved'];
$queryusername = $query['username'];
if ( $queryapproved > 0 )
{
$queryapproved = "true"
echo "welcome, you are approved to see this screen";
} else {
$queryapproved = "false"
echo "restricted";
}
?>
<?php
$query = "SELECT approved,username FROM members where username=$username";
$queryapproved = $query['approved'];
$queryusername = $query['username'];
if ( $queryapproved > 0 )
{
$queryapproved = "true"
echo "welcome, you are approved to see this screen";
} else {
$queryapproved = "false"
echo "restricted";
}
?>
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)) // found a user with that name
{
$info = mysql_fetch_assoc($result);
$approved = $info['approved'];
$username = $info['username'];
// ..........
}
else
// you can't see page.
<?php
$query = "SELECT approved,username FROM members where username='$username'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)) // found a user with that name
{
$info = mysql_fetch_assoc($result);
$approved = $info['approved'];
$username = $info['username'];
//
if ( $approved > 0 )
{
$queryapproved = "true"
echo "welcome, you are approved to see this screen";
} else {
$queryapproved = "false"
echo "restricted";
}
}
?>
How would I set this so that if I am not a user logged in using the table at all it would display a default message. I have to be logged in with either a value set to 1, which is approved, or 0, which is the default, but I can't get anything to display if I am not logged in at all.... thoughts?
<?php
$query = "SELECT access,username FROM members where username='$username'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)) // found a user with that name
{
$info = mysql_fetch_assoc($result);
$approved = $info['access'];
$username = $info['username'];
//
if ( $approved!="0" ) {
include "blog.inc.php";
} elseif ( $approved="0" ) {
include "noblog.inc.php";
} else {
include "noblog.inc.php";
}
}
?>
Yup.. I am using sessions.. so that's a plus. The thing is that once you hit the web site, a session is already created. How might I use this to my advantage?
//assume that the user has logged in through a form that was posted. $username and $password were passed
$sql = "SELECT * FROM users WHERE username='".mysql_escpae_string($_POST['username'])."' AND password ='".mysql_escape_string($_POST['password'])."'"; //probably a bad idea to have the passowrd in plain text, but anyway
$result = mysql_query($sql, $db_connection);
if (mysql_num_rows($result)==1) {
session_start();
$_SESSION['username']=$_POST['username']; }
else {
//fail the login
}
The idea here is that on any page on your site where you have the session_start(); command at the top, you can access the value of $_SESSION['username']. You can do it for as many variables as you need using the $_SESSION superglobal array.