I'm trying "CheckBot" and I have an observation. The problem is that in my PHP code I have an if to check if the URL exists in a table called "seo_url" if it does not exist then we show the content of error.tpl
<?php
require_once 'app/config.php';
$url = isset($_GET["url"])?$_GET["url"]:'index';
$querytcseos = DB::queryFirstRow("SELECT * FROM tc_seos WHERE seo_url = %s LIMIT 1", $url);
require_once 'includes/header.tpl';
if (!$querytcseos['seo_file']) {
include 'includes/error.tpl';
} else {
include 'includes/'.$querytcseos['seo_file'].'.tpl';
}
require_once 'includes/footer.tpl';
?>
I see that this does not seem to be the right thing to do since "CheckBot" when you enter example.com/page-not-fount if you are finding content.
How do I get my error page taken as error 404?
http_response_code(404);
For best practice. You should respond it like that and let the server (Apache/Nginx/IIS) to handle the 404 page.
Related
I have take the page.php page and i have created a new template page named mytemplatepage.php
The page mytemplatepage.php works normally untill i try to add an sql query. when i add the bellow code the page gives http 500 error.
<?php
global $wpdb;
$sqlresults = $wpdb->get_results(
"SELECT id, CategoryName
FROM wp_SimParts"
);
?>
if i remove the code and just leave
<?php
?>
the page load normally. if i put an echo on the php code the page again crashes with http 500 error
<?php
echo "hello there";
?>
--EDIT Error Log--
I am taking this error from logs
PHP Parse error: syntax error, unexpected 'my_template_page' (T_STRING) in
I think you need to try like this.
You write below method on your template file.
function my_template_page() {
global $wpdb;
$query = "SELECT id, CategoryName FROM wp_SimParts";
$pageposts = $wpdb->get_results($query, OBJECT);
$page = (array) $pageposts ;
if(!empty($page)) {
return "Not Empty";
} else {
return "Empty";
}
}
echo my_template_page();
If you get error then either you did mistake on header file content-type OR you need to update your WordPress. Your template file is fine.
Thanks
error 500 is an internal server error, check all you closing tags in your mytemplatepage.php template page, also check your error log on the server.
It would help it you copy paste the error here
I'm working with a page that, once a link is called this script checks and if the POST contains the keyword it and then finds that page. However no matter how I organize this if it doesn't work.
<?PHP
if($_POST['page']) {
$page = (int)$_POST['page'];
$exists = file_exists('pages/page_'.$page.'html');
if($exists) {
echo file_get_contexnts('pages/page_'.$page.'html');
} else {
echo 'There is no such page!';
}
} else if ($_POST['course']) die("0"); {
$course = (int)$_POST['course'];
$exists = file_exists('courses/course_'.$course.'html');
if($exists) {
echo file_get_contexnts('courses/course_'.$course.'html');
die("1");
} else {
echo 'There is no such page!';
}
}
?>
The error I'm currently receiving with this setup is:
Notice: Undefined index: course in C:\wamp\www\Home Page\load_page.php on line 12
Call Stack
# Time Memory Function Location
1 0.0003 253944 {main}( ) ..\load_page.php:0
Is it because there is no 'course' in the page? I might be confused of the code I'm modifying a tutorial of a simple ajax website. It is possible what I am trying to do does not work.
In that case how could I possible go about doing what I want to do.
Right now I have a home page and it loads in another page without switching pages. I like the floridness of it. I would like to have a sort of sub call. So if you are on the home page and you go to courses page then you can click on a specific course and that will load from a different directory within the courses directory.
Homepage (when you click on courses you go to...)
pages/courses_home.html (when you click on a course you go to...)
courses/course_1.html (you can view course and then click back to directory above or go to home)
That is the structure I'm looking to try to achieve.
If more information is needed please let me know what and I'll do my best to include it. Thank you.
The syntax should be:
if(isset($_POST["page"])) {
} elseif(isset($_POST["course"])) {
}
I am not sure why you have a die statement there, but I don't think it belongs. Also, keep in mind the logic for what happens if neither of these conditions is met.
Edit: also keep in mind that isset doesn't prevent empty strings, so you may want to check for that as well. A function you could use is
function checkPost($value) {
return isset($_POST[$value]) && $_POST[$value] !== "";
}
To use:
if(checkPost('page')) {
//some logic
}
Wrong syntax.
elseif ($_POST['course']) {
without die statement.If 'course' undefined else statement works and does not get error. Sorry for bad English.
Try this:
if isset(($_POST['page'])) {
...
} else if isset(($_POST['course'])) die("0"); {
instead of this:
if($_POST['page']) {
...
} else if ($_POST['course']) die("0"); {
I have a little problem with a PHP warning:
I basically want to change the content of my page by clicking on links, like this:
<?php $page = ((!empty($_GET['page'])) ? $_GET['page'] : 'home'); ?>
<h1>Pages:</h1>
<ul>
<li>News</li>
<li>F.A.Q.</li>
<li>Contact</li>
</ul>
<?php include("$page.html");?>
This works really fine, but when I use a page that doesn't exist, for example
localhost/dir/index.php?page=notapage i get following error:
Warning: include(notapage.html): failed to open stream: No such file or directory in
C:\xampp\htdocs\dir\index.php on line 8
Warning: include(): Failed opening 'notapage.html' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\dir\index.php on line 8
Is it possible to replace this warning by a custom message? (like a "404 not found")
Thanks in advance and happy easter!
You could use file_exists() but keep in mind that your approach is not very safe.
A safer approach would be using an array with allowed pages. This way you have a better control over user input. Something like this:
$pages = array(
'news' => 'News',
'faq' => 'F.A.Q.',
'contact' => 'Contact'
);
if (!empty($pages[$_GET['page']])) {
include($_GET['page'].'html');
} else {
include('error404.html');
}
You could also generate the menu using that array.
You can do
if (file_exists($page.html)) {
include("$page.html");
}
else
{
echo "404 Message";
}
Source: PHP Manual
You can check if the file exists() and then include a custom 404 template.
<?php
if (file_exists($page + '.html')) {
include ($page + '.html')
} else {
include ('404.html');
}
?>
The idea is to check if the file exists before trying to include() it:
if(!file_exists("$page.html"))
{
display_error404();
exit;
}
include("$page.html");
Yes it is possible, though I would recommend sending a 404 unless you are going to also use clean url's (like /news, /faq, /contact) that redirect behind the scenes to the index.php, writing the page parameter. This is because index.php really does exist, you just have a bad parameter. Thus a 404 would not be appropriate. This is not to mention that you actually can;t set a 404 header in this location anyway since you have already sent output to the browser.
For you case just set up a conditional on whether the file_exists and is readable like this:
$include_file = $page . '.html';
if (file_exists($include_file) && is_readable($include_file)) {
include($include_file);
} else {
// show error message
}
I'm aware that this topic has been covered before here on Stack, and I have looked at some answers, but I'm still a bit stuck, being fairly new to PHP. Every page on my website requires a login, and so users are redirected to a login page on page load. At the top of each page then I have:
<?
require("log.php");
include_once("config.php");
include_once("functions.php");
?>
This redirects the user to log.php (with new code added):
<?
session_name("MyLogin");
session_start();
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
if($_GET['action'] == "login") {
$conn = mysql_connect("localhost","",""); // your MySQL connection data
$db = mysql_select_db(""); //put your database name in here
$name = $_POST['user'];
$q_user = mysql_query("SELECT * FROM users WHERE login='$name'");
if (!$q_user) {
die(mysql_error());
}
if(mysql_num_rows($q_user) == 1) {
$query = mysql_query("SELECT * FROM users WHERE login='$name'");
$data = mysql_fetch_array($query);
if($_POST['pwd'] == $data['password']) {
$_SESSION["name"] = $name;
header("Location: http://monthlymixup.com/$url"); // success page. put the URL you want
exit;
} else {
header("Location: login.php?login=failed&cause=".urlencode('Wrong Password'));
exit;
}
} else {
header("Location: login.php?login=failed&cause=".urlencode('Invalid User'));
exit;
}
}
// if the session is not registered
if(session_is_registered("name") == false) {
header("Location: login.php");
}
?>
The login form is contained in login.php. The code for login.pho relevant to the PHP/log.php is:
<?
session_start();
if($_GET['login'] == "failed") {
print $_GET['cause'];
}
?>
and
<form name="login_form" id="form" method="post" action="log.php?action=login">
The answer that I came across stated that I should add:
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI'];
to the top of each page, which I did, at the top of the page (above "require("log.php");"), and then add:
if(isset($_SESSION['url']))
$url = $_SESSION['url']; // holds url for last page visited.
else
$url = "index.php"; // default page for
to my login page, and use the following URL for redirect on successful login:
header("Location: http://example.com/$url"); // perform correct redirect.
I am not 100% where the code which stores the referring URL should go, at the top of log.php or login.php.
I have tried adding it to both, but the login page is just looping once I have entered the username and password.
I wonder if someone could help me get this working?
Thanks,
Nick
It appears that I don't have the privilege to comment on your post, so I'll do the best that I can to answer. I apologize for all of the scenarios, I'm just doing the best I can to answer on a whim.
SCENARIO 1:
If you've truly not selected a database in your code, as demonstrated here, could that potentially be your issue? Please do note, that the code below, is the code you've posted.
$db = mysql_select_db(""); //put your database name in here
SCENARIO 2:
The code below is not something I've ever used in anything I've built, might I suggest that you try replacing that line of code with the line below it?
if(session_is_registered("name") == false) { // Current
if(isset($_SESSION['name']) == false) { // Potential Replacement
SCENARIO 3:
If you're logic for the following, exists on the login.php file as well... That could potentially be your problem. Upon visiting your site, I noticed your form appears on login.php, yet your logic is posting to log.php. I'm hoping this bit of code can help rule out that "jump", as login.php might be saving itself and overwriting the $_SESSION variable you've established
session_start(); // starts the session
$_SESSION['url'] = $_SERVER['REQUEST_URI'];
If it's too complex to take it out of the login.php file, if you even have it there, I've put together some code that you can use to create "internal" breadcrumbs, so you can go 2 pages back in your history.
if(!isset($_SESSION['internal_breadcrumbs']))
$_SESSION['internal_breadcrumbs'] = array();
$_SESSION['internal_breadcrumbs'][] = $_SERVER['REQUEST_URI'];
$max_breadcrumbs = 5;
while(count($_SESSION['internal_breadcrumbs']) > $max_breadcrumbs)
array_shift($_SESSION['internal_breadcrumbs']);
That will create an array with a max of $max_breadcrumbs elements, with your most recent page at the end, like the following
Array
(
[internal_breadcrumbs] => Array
(
[0] => /other_page.php
[1] => /other_page.php
[2] => /other_page.php
[3] => /user_page.php <-- desired page
[4] => /login.php <-- most recent page
)
)
So now... you can setup your url to be something more like the following...
// I'm doing - 2 to accommodate for zero indexing, to get 1 from the current page
if(isset($_SESSION['internal_breadcrumbs']))
$url = $_SESSION['internal_breadcrumbs'][count($_SESSION['internal_breadcrumbs']) - 2];
else
$url = "index.php"; // default page for
All the best, and I certainly hope this has helped in some way.
IN SCENARIO 4
From the client test the login/password which ajax XMLHttpRequest with javascript code to a dedicated script for validation (do it on mode https for secure)
If response is right send the login password to your script server.
Stips : Encoding password is better secure !
Using header() function it's a bad idea.
Manual specification say ;
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
So in your case, i suggest that to use cookies with an ID generate only for the session, at the first connection its generate, and the duration of the cookie maybe for only from 2 to 10 minutes.
Regenerate cookie each time the loging.PHP is called !
Have a nice day
I was curious to know if i could do this, but found no examples on line
$info_find = mysql_query("SELECT info FROM sets WHERE category = '$selected_cat'")
or die( header ("location: index.php"));
Firstly doing it as above doesn't work. Can it be done?
Are there any drawbacks?
die() just writes its parameter out, it doesn't execute it. So a "or die()" construct will not help you there.
You can consider something like
or die(createRedirect("index.php"));
with
function createRedirect($where) {
$s='<html><head>';
$s.='<meta http-equiv="refresh" content="1; url='.$where.'">';
$s.='</head><body>If you are not automatically redirected click here';
$s.='</body></html>';
return $s;
}
if you are willing to accept the downsides of client-sided redirection
You can rewrite your code as
$info_find = mysql_query("SELECT info FROM sets WHERE category = '$selected_cat'") ;
if ($info_find === FALSE) {
header ('location: index.php');
die();
}
But, before use the header function, be sure you haven't send any output to the browser.