.htaccess rewrite rule page not found - php

If page name doesn't exist then I would like to show page not found instead of a blank white page.
.htaccess:
RewriteEngine On
RewriteRule ^([0-9a-z\-\_]+)?$ page.php?name=$1 [L,QSA,NC]
page.php:
$name = filter_input(INPUT_GET, 'name');
$result = $mysqli->query("SELECT * FROM pages WHERE name='$name'");
while ($row = $result->fetch_assoc()) {
echo $row['content'];
}
If some of you know a better way, please share it :)

You can change your PHP code like this:
$name = filter_input(INPUT_GET, 'name');
if ($result = $mysqli->query("SELECT * FROM pages WHERE name='$name'")) {
if ( $result->num_rows == 0 ) {
printf('404 / page Not Found');
exit;
}
else {
while ($row = $result->fetch_assoc()) {
echo $row['content'];
}
}
} else {
printf("Error: %s\n", $mysqli->error);
exit;
}

You can get the mysql num rows and make if it == 0 then show page not found before the while loop

Related

Can I have 2 header locations in PHP?

So, I know my code was working and I have tried un-doing all steps to see where the bug is, but I am still keep getting an error
my php id=0.
Can you guys show me how I can fix my code up?
The error is as follows:
undefined variable list_id. It works on my localmachine but not when
uploaded to server.
Thanks.
The following is my code:
if(!empty($_GET['id'])){
$list_id = intval(($_GET['id']));
try {
$sql = 'SELECT * FROM items where id =' . $list_id;
$query = $pdo->prepare($sql);
$query->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$list = $query->fetch(PDO::FETCH_ASSOC);
if ($list == FALSE) {
header("location: index.php");
}
}
if ($list_id == 0) {
header("location: index.php");
}
Seems there are a few issues here. I have added the comments inline.
if(!empty($_GET['id'])){
$list_id = intval($_GET['id']); //was double parenthesis here
try {
$sql = 'SELECT * FROM items where id =' . $list_id;
$query = $pdo->prepare($sql);
$query->execute();
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$list = $query->fetch(PDO::FETCH_ASSOC);
$count = count($list); //count result and use for comparison instead of false
if ($count === 0) {
header("location: index.php");
exit;
}
} else {
header("location: index.php"); //if no $_GET, redirect
exit;
}
All you need to do is instantiate the variable $list_id :
$list_id = 0; // <-- HERE
if(!empty($_GET['id'])){
$list_id = intval(($_GET['id']));
...
...
It looks as though you want to redirect to the index if there is no id parameter. Since you already check for its presence, redirect in the else clause. Remove the last block and add:
else
{
header("location: index.php");
exit;
}
You may want to add exit; after the header() call in the if block as well, so that code that uses the database further down isn't executed.

URL friendly from DB, php and .htaccess

can help me?
I've a site url like:
http://www.example.com/episodio/ID
ID = row number from Database.
I want to display the page title.
I've this:
.htaccess file:
RewriteEngine on
RewriteRule ^episodio/(\w+)/?$ episodio.php?id=$1
php file:
$sql = "SELECT * FROM "episodios" WHERE serie = ".$id." AND temporada = ".$i." ORDER BY "episodio" ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row['episodio'] == 0) {
echo '<li><strong>Episodio doble:</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li>';
}else{
echo '<li><strong>Episodio '.$row['episodio'].':</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li>';
}
?>
<?php } ?>
You're using $_GET in your ID anywhere before this code?
I mean, $_GET['id'].
Most PHP versions (since 4...) doesn't catch the var just for its name.
I'd recommend getting all your variables from the DB first, and then printing out the HTML (perhaps from a template file). We assume that $row contains more than just an ID? Perhaps an "título del episodio", also?
if ($result->num_rows > 0) {
$episodios = array();
$x = 0;
while($row = $result->fetch_assoc()) {
$episodios[$x]['id'] = $row['id'];
if ($x == 0) {
$PageTitle = $row['title'];
}
$x++;
}
Then your template can use $PageTitle (from the first episode), and you can loop through the $episodios array to construct the links for the other episodes.

Check if row in table is 'equal' to other row

I have the following code to check if a row exists in MySQL:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT 1 FROM files WHERE id='$code' LIMIT 1");
if (mysql_fetch_row($result)) {
echo 'Exists';
} else {
echo 'Does not exist';
}
}
?>
This works fine. But I need to change it a bit. I have the following fields:
id, title, url, type. When someone uses the code above ^ to check if a row exists, I need a variable to get the url from the same row, so I can redirect the user to there.
Do you have any idea how I can do that?
Thanks in advance! :)
Try this:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT * FROM files WHERE id=" . $code . " LIMIT 1");
if (mysql_num_rows($result) > 0) {
while($rows = mysql_fetch_array($result)) {
echo 'Exists';
$url = $rows['url'];
}
} else {
echo 'Does not exist';
}
}
?>
It is quite simple. I think you don't show any effort to find the solution by yourself.
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT url FROM files WHERE id='$code' LIMIT 1");
if ($result) {
$url = mysql_fetch_row($resultado);
} else {
echo 'Does not exist';
}
}
<?php
$sql_query = "SELECT * FROM test WHERE userid ='$userid'";
$result1 =mysql_query($sql_query);
if(mysql_num_rows($result1)>0){
while($post = mysql_fetch_array($result1))
{
$url = $post['url'];
}
}
?>
If mysql_num_rows($result1)>0 it means row is existed fir the given user id

Notice Undefine Offset PHP

I'm trying to redirect a page if the admin changes the page to not visible, by changing the visible value to zero, what is wrong with my code, i keep getting the error
<?php
$result = mysqli_query($con,"SELECT VISIBLE FROM menu WHERE id=0");
if($row = mysqli_fetch_array($result))
{
header('Location: ./unavailable.php? error=pagenotavaialbe');
}
else
?>
Try something like this
$result = mysqli_query($con,"SELECT VISIBLE FROM menu WHERE id=0");
if(mysqli_num_rows($result) == 0){
header('Location: ./unavailable.php? error=pagenotavaialbe');
exit;
} else {
$row = mysqli_fetch_array($result);
//do your code here
}
try this :
if($result)
{
if($row = mysqli_fetch_array($result))
{
header('Location: ./unavailable.php? error=pagenotavaialbe');
}
}

MySQL PHP record checking

im having a simple mysql/php problem. so i am adding in Image titles for my website, and the code is displayed below. It works, but when you dont put a image, it shows up as blank. I need it to show up as 'No image title' (bc i will use this for image description to). It basically gets the image name, then takes the title from that row.
So how do i do it? :/ im still very new to PHP.
<?php
if (isset($imgtitleset))
{
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result))
{
$imgtitle= $row["image_title"];
echo "$imgtitle";
}
}
else {
echo 'no image title';
}
?>
Change the while loop like so:
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
Also, I'm not sure what the $imgtitleset variable is for, but you can probably get rid of the if statement checking to see whether it's set.
Edit: the whole thing should probably look like this:
<?php
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
$imgtitle= $row["image_title"];
if($imgtitle != '') {
echo $imgtitle;
} else {
echo 'no image title';
}
}
?>
This all depends on what $imgtitleset is equal to. It is clearly set against something:
while ($row = mysql_fetch_array($result)) {
$imgtitle = $row["image_title"];
if (isset($imgtitle))
echo "$imgtitle";
else
echo 'no image title';
}
This would mean if nothing was found in the database then it will echo the no image title. However like I said, this could depend on what $imgtitleset is, maybe post the code for that?
If you only expect the select to return a single row, then use if rather than while and return the error on else:
<?php
if (isset($imgtitleset))
{
$sql = "SELECT image_title FROM images WHERE image_name = '$image_main'";
$result = mysql_query ($sql);
if ($row = mysql_fetch_array($result))
{
$imgtitle= $row["image_title"];
echo "$imgtitle";
}
else {
echo 'no image title';
}
}
?>

Categories