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 have adverts on the site but i have set permission on the adverts to be either 'public' or 'restricted' so based on that i want to create a statement with three possible outcomes, it should check the values of the columns and if the is an advert it will check the permission if the permission 0 (public) it shows the ad if permission 1 (restricted) it will direct the user to a log-in page however if the user is logged on then it will just show all the adverts.
These is the code i'm using but it is only executing the else line (so when the user clicks on the ad it takes them straight to the login page even if the ad is set to '0')....i checked the error log and i get the msg :PHP Notice: Undefined variable: SiteDir in
<?php
# Get all records from the advert table
$qry_getAdverts = mysql_query("SELECT `advertID`, `accesslevelID` FROM `adverts_adverts`;");
# Select a single record
$row_getAdvert = mysql_fetch_assoc($qry_getAdverts);
# I assume this line is a typo? $qry_getAdverts is a resultset, you need to pull records from this.
//if($qry_getAdverts["AdvertID"] > 0 && ($qry_getAdverts["accesslevelID"] == 0 )) {
# This is kind of silly. You should be putting this type of stuff into your SQL query.
// Ex 1: "SELECT `advertID`, `accesslevelID` FROM `adverts_adverts` WHERE `advertID` > 0;"
// Ex 2: "SELECT `advertID`, `accesslevelID` FROM `adverts_adverts` WHERE `advertID` > 0 AND `accesslevelID` = 0;"
if($row_getAdvert["AdvertID"] > 0 && ($row_getAdvert["accesslevelID"] == 0 )) {
# How can we echo something that we haven't queried?
// Modify Query to: "SELECT `advertID`, `accesslevelID`, `Content` FROM `adverts_adverts`;"
echo($row_getAdvert["Content"]);
} else {
# This is also very silly. What if the user has disabled javascript on their browser? Lets just send a HTTP header instead.
//echo '<script type="text/javascript">window.location.href=\'' . $SiteURL . '/my-account/login.php\';</script>';
header("location: {$SiteURL}/my-account/login.php");
exit();
}
?>
Thanks for the reply and thanks for correcting my mistake of using action script to re-direct the user. I have another question
I can't user headers because the script already uses other set headers which then conflict if i add another one on the if statement. my question is how can i direct the user to a particular page 'login' from the if statement without using javascript or headers? and if i have to use header how do i set it up so it doesn't conflict with the others already set?
Yes i get this: PHP Warning: Cannot modify header information - headers already sent by (output started at /.../.../.../.../inc_header.php:6) in /..../..../..../..../adverts.php on line 64
And it won't take me to the login page instead it just shows half the advert and no footer which makes me suspect it is stopping the code there.
jose07 wrote:Yes i get this: PHP Warning: Cannot modify header information - headers already sent by (output started at /.../.../.../.../inc_header.php:6) in /..../..../..../..../adverts.php on line 64
And it won't take me to the login page instead it just shows half the advert and no footer which makes me suspect it is stopping the code there.
Yup, as I suspected.
You must make all header calls before sending any output. Output can be anything from part of your HTML code to a blank space or extra line in your php documents. You must be conscious of this when writing your code. A bit of a crutch that can make life easier is to use output buffering. One of the first lines of code in your project could be: "ob_start();" This will buffer all output and allow you to send headers even if you've accidentally spit out output before sending the header.
This is actually a VERY common problem with newer PHP developers. Search this forum and you will find an abundance of information on the dreaded "header()" call.
Thanks for that, you're brilliant! compared to all the other forums your replies were the only ones that actually helped me solve the problem, so thanks for taking time to explain. The ob_start(); worked beautifully. Thanks again and keep up the good work!