PHP Main page script with GET Won't work - php

I've made a simple index.php script for my homepage, however, it won't work.
<?php
$Query = $_GET['p']
if (strtolower($Query) == "about") {
include("About.php")
die()
}
if (strtolower($Query) == "") {
include("Home.php")
die()
}
if ($Query) {
include("Home.php")
die()
}
?>
It's my first time coding in php so i don't know what is wrong.

First you need simicolons. Second, why die? Why not do something like.
<?php
$Query = $_GET['p']
if (strtolower($Query) == "about") {
include("About.php");
} else {
include("Home.php");
}
?>

Related

if statement and isset($_GET if statement error with the ifelse

<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '1'): ?>
<?php include('pages/home.php');?>
<?php endif;?>
<?php };?>
<?php if (isset($_GET['p'])) { $variable = $_GET['p']; ?>
<?php if ($variable == '2'):
include('pages/about.php');
else: include('pages/home.php'); endif; };?>
The above is what I have used to try and fix it, but if I don't put it, errors show up. For example if I used "ifelse", and it tells me to change it to else, or endif. But when I use endif, it tells me to use ifelse. I'm trying to make it so http://localhost/?p=PAGE_ID just simply shows the page using an include(FILE_PATH) statement.
Does anyone know where I went wrong? Or how I can fix it :)
You seem to have thoroughly tied yourself in knots. To my mind, this is a much cleanrer and more flexible and maintainable way to approach the whole thing:
$pages = [
"1" => "pages/home.php",
"2" => "pages/about.php"
];
if (isset($_GET['p'])) {
if (isset($pages[$_GET['p']]))
{
$page = $pages[$_GET['p']];
}
else
{
$page = $pages["1"];
}
}
else
{
$page = $pages["1"];
}
include($page);
This way, you have a list of pages, and the system simply looks up the required page in the array by its index number (as passed in via the GET parameter). If it can't find that index, it just uses the default page.
You can now easily add pages by just adding items to the array, without needing to write more if/else logic. You could even store the array data in a database instead of it being hard-coded.
You dont need or want an elseif in this flow, simple do
<?php
if (isset($_GET['p'])) {
if ($_GET['p'] == '1') {
include('pages/home.php');
}
if ($_GET['p'] == '2') {
include('pages/about.php');
}
}
If the value of $_GET['p'] can only be 1 or 2 you could do
<?php
if (isset($_GET['p'])) {
if ($_GET['p'] == '1') {
include('pages/home.php');
} else {
include('pages/about.php');
}
}
Or if $_GET['p'] could be multiple values a switch may be more useful
<?php
if (isset($_GET['p'])) {
switch($_GET['p']) {
case 1:
include('pages/home.php');
break;
case 2:
include('pages/about.php');
break;
default:
include('pages/somethingelse.php');
}
}
Note I also left out the intermediate variable, as its not needed. The $_GET and $_POST arrays are, once filled by PHP all yours to use as you want. There is no need to waste CPU cycles and memory creating unnecessary variables

php and $_GET array

I have submitted some code to the redirected url and now trying to use this to echo some information out but can't figure out where I am going wrong.
I have the following code:
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt) == '1'{
return 'failed';
}
?>
all I want to do is if the url has $login_attempt=1 I want to return the message 'failed' to the page.
There is no point of escaping anything if it doesn't enter anywhere important (like a database).
<?php
if ($_GET['login_attempt'] == '1') {
echo 'failed';
}
?>
Also, you have a problem in your if statement, that's corrected in the code above. Be sure to include all of the condition inside of parenthesis, and not just one side of the equality check.
Also, if you wish to display something on the screen, you should echo it, not return.
how about:
if ($login_attempt == '1'){
echo 'failed';
}
Try this one. Your error in $login_attempt == '1':
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1'){
echo 'failed';
return false;
}
?>
As others already mentioned you have several problems but the syntax error comes from this:
if ($login_attempt) == '1'{
it should be
if ($login_attempt == '1') {
Dont u think if ($login_attempt) == '1' should be something like this ($login_attempt == '1') Sorry...many others also suggested this :P
At the first, I must tell you that you have a mistake in your IF condition. You typed == outside of ().
In addition, you have to be aware of status of setting your variable through your URL. Check the code below. In this code, I made a function to check the status. Default status is true, and we will check it just for a negative condition. I hope it could be useful for you:
<?php
function check() {
if (isset($_GET['login_attempt'])) {
$login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1') {
return false;
} else {
return true;
}
} else {
return true;
}
}
if (!check()) echo('Error Message');
?>

Need help checking if else statement for { }

Somewhere along the line I'm adding or leaving out a { } but I just can't figure out where
<?php
if (file_exists('config.php')) {
require_once('config.php');
{
if ( $EDITED_CONFIG == false )
{
header("Location: welcome.php");
}
}
}
else (file_exists('default-config-new.php')) {
require_once('default-config-new.php');
{
if ( $EDITED_CONFIG == false )
{
header("Location: welcome.php");
}
}
}
?>
If file exists require it and if edited = false redirect, if true end script.
else
If file exists require it and if edited = false redirect, if true end script.
So if the first file doesn't exist it mustn't require it or look for edited, it must skip to the second file and if that exists it must checked edited and then if is false then redirect. If the first file is true it must end script and load page. So it mustn't check second file if first file is true.
Also is this the lightest way to do this?
Thanks
If you indent your code properly, your error will become evident.
A few links that may be useful:
Wikipedia: Indent style
How to indent code
PHP Coding standard: Indentation
You're not closing your if statements: Should be something like:
<?php
if (file_exists('config.php')) {
require_once('config.php');
if ($EDITED_CONFIG == false) {
header("Location: welcome.php");
}
}
else{
require_once('default-config-new.php');
if ($EDITED_CONFIG == false) {
header("Location: welcome.php");
}
}
?>
Edited. Also, you need to close brackets around all code to be executed for that statement, before you can use another elseif or else statement:
if ($x == 1) {
echo "X is 1!";
}
else if ($x == 0) {
echo "X is 0!";
}
else {
echo "Not 1 or 0!";
}
You are missing the { after the else to enclose what you want inside the "else" block i believe
You need to write
else if (conditions...)
You have got
else (conditions...)

How to change the elements in the page base on user who is logged in

Please give me an idea on how to display elements in a page depending on who is logged in. For example, a user or an administrator.
I'm thinking of something like this but I get a parse error, what do I lack in this code?:
EDIT:
<?php
session_start();
if (!(isset($_SESSION['loginAdmin']) && $_SESSION['loginAdmin'] != '')) {
header ("Location: loginam.php");
}
else if (!(isset($_SESSION['loginAdmin']) && $_SESSION['loginAdmin'] =='')) {
include('head2.php');
}
else if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: login.php");
}
else if (!(isset($_SESSION['login']) && $_SESSION['login'] =='')) {
include('head3.php');
}
?>
Please help, there's no error but its not functioning properly. Whenever I try to access the page where I have this code. And login as a user. It redirects to loginam.php(the page where the admin will login). But there's no problem when I log in as admin. It works properly. What do I do?
Use
if (condition)
{
}
else if (condition) {
}
Also Just to make things simpler .. try something like ..
function is_admin() {
if(isset($_SESSION['loginAdmin']) && $_SESSION['loginAdmin'])
return true;
} else {
return false;
}
}
and then check
if(is_admin()) {
///admin block
} else {
//admin login
}
if(is_user()) {
///user block
} else {
//user login
}
if else is not valid. It's else if.
Other than that, it would help if you posted the parser error along with your code.
You're also not closing your <?php statement before opening it again.
because you have an invalid code.
he's the trimmed code:
&lt?php
session_start();
if (!isset($_SESSION['loginAdmin']) && ($_SESSION['loginAdmin'] != '')) {
header ("Location: loginam.php");
} else {
include('head2.php');
}
?>
&lt?php
if (!isset($_SESSION['login']) && ($_SESSION['login'] != '')) {
header ("Location: login.php");
} else {
include('head3.php');
}
?>
pay a little attention to the code.

IPB's page controller

Anyone knows how Invision Power Board makes their urls like the following?
index.php?showuser=349
index.php?showtopic=83
index.php?showforum=9
and just pages:
index.php?act=register
index.php?act=about
and so on. How they do it? I'm sure they don't do it like i do now:
if (isset($_GET['showtopic'])){
include('viewtopic.php');
else if (isset($_GET['showuser'])){
include('viewuser.php');
}
else if (isset($_GET['act']) && $_GET['act'] == 'register'){
include('register.php');
}
else if (isset($_GET['act']) && $_GET['act'] == 'about'){
include('about.php');
}
else
{
echo "page not found.";
}
To do the "act=" thing you don't need a huge chain of if statements. You could do it like this, for example:
$pages = array('register', 'about', ...);
if (in_array($_GET['act'], $pages)) {
include $_GET['act'].'.php';
} else {
// display an error
}
Maybe you could add it to an array.
$pages = array('showtopic', 'showuser');
foreach ($pages as $page) {
if (intval($_GET[$page])) {
include("$page.php");
}
}

Categories