php var isset, ?data=menu - php

I have script where some url leads to index.php?data and the page opens
how can I use that to use the url open an "subpage" like index.php?data=menu or so?
the code I use with the first one is
if(isset($_GET['palvelut'])){ echo "this is a sample"; }

You have the right idea, but allow me to elaborate at little on this. You need to check if $_GET['data'] is set, by doing isset($_GET['data']), and if that is set, checking to see if it has a given value, "menu" in this case. You can do that like this $_GET['data'] == "menu". Putting it all together, you get this:
if (isset($_GET['data']) && $_GET['data'] == "menu") {
/* Menu code goes here */
}
If you would like to have this work for multiple values for data you can do the following:
if (isset($_GET['data'])) {
switch($_GET['data']) {
case "Possible_Value_1" :
/* Code for this condition appears here */
break;
case "Possible_Value_2" :
/* Code for this condition appears here */
break;
/* etc... */
default :
//Just as a precaution...
echo "Invalid 'data' value supplied!";
break;
}
}
Hope that helps.

If I understand your question (which I don't), you've answered it already.
Your URL is: http://www.mydomain.com/index.php?data=something
Your code would be:
if (!isset($_GET['data'])) {
//do something because no data argument was passed
} else {
switch ($_GET['data']) {
case "homepage":
header("location: homepage.php");
die;
break;
case "someotherpage":
header("location: someotherpage.php");
die;
break;
//and so on
}
}
Obviously instead of using a header redirect, you might just require() or include() a file, or do something else entirely.

Related

How to return defined constant with header function

I have my validation system and I want to make if something isn't ok it's return for example EMPTY but EMPTY is defined as function header.
I tried something like below but it is used all the time, not only when name and pass fields are empty. I could just write if... { header...} but i don't want like that. How to make it?
include 'errors.php';
if(empty($name) || empty($pass){
return EMPTY;
exit();}
in another file: errors.php
define('EMPTY', header("Location: ../index.php?error=empty"))
it is used all the times because when you write
define('EMPTY', header("Location: ../index.php?error=empty"));
the header function is run and its return value is assigned to the constant EMPTY.
If you want to have specifical functions running on errors and you want to define them in error.php, you should have
//error.php
function got_error($cause){
switch($cause){
case 'EMPTY':
header("location:../index.php?error=empty");
break;
case 'SHORT':
header("location:../index.php?error=short");
break;
default:
header("location:../index.php?error=unknown");
}
exit();
}
include 'errors.php';
if(empty($name) || empty($pass)){
got_error('EMPTY');
}
if(strlen($pass) < 5){ //example of other error
got_error('SHORT');
}

Undefined index when user returns in same session

I have a simple login system with sessions. The user is being redirected to a page when the user succesfully logs in.
The problem is when the user leaves my site and comes back later to index.php (the same session) the user will get "Undefined index" because there's no parameter supplied when the user enter my site and is still logged in.
I use php switch to control my pages.
I have this code first in my index.php:
require_once('function.php');
session_start();
if (!is_user()) {
redirect('signin.php');
}
?>
My file with switch looks like this:
<?php
$p=$_REQUEST['p'];
if (isset($p)) {
switch ($p) {
case "vine":
include "vine.php";
break;
}
?>
Obviously $_REQUEST['p'] is undefined.
If you want your script to still know the p parameter when a user returns, you must somehow save it for further requests. This could be done like this in index.php:
<?php
session_start();
$p = isset($_REQUEST['p']) ?
$_REQUEST['p'] : (
isset($_SESSION['p']) ?
$_SESSION['p'] :
false
)
);
if ($p !== false) {
$_SESSION['p'] = $p;
switch ($p) {
case "vine": include "vine.php";
break;
}
} else {
die ('Unknown category ....');
}
?>
The code looks for an explicitely given parameter p and takes this if available. Otherwise it looks for a session parameter p.
Else it sets p to false to indicate that no value is avaible.
If a value for p is given, the session variable $_SESSION['p'] is set. And, of course, sesssion_start() must be called at the top of the script to make session variables available.
I assume the 'Invalid index' comes from $p=$_REQUEST['p'];. You want to check whether that array element exists.
if (isset($_REQUEST['p'])) {
What about this:
if (isset($_REQUEST['p']))
{
$p = $_REQUEST['p'];
// ...
}
But notice this: http://php.net/manual/en/function.array-key-exists.php#example-5520

Access all files from separate folders

How can I access all the files from a folder for example:
I added my site files to a folder. In the root of the website, I have the folder that consist of my files, index.php and my theme menu. So my question is,
How can I access my files in that folder from a specific link in the menu?
I don't want them to be accessed like "myste.com/sections/forum.php" rather I need to know how to access them like "mysite.com?page=forum" or something like a CMS. I really appreciate if someone can help me with this. I havE been searching for solution but no luck.
Thanks.
The command mysite.com?page=forum will run the index.php file in the mysite root folder.
So you need to write some code in the index.php file to redirect to the correct place, like
<?php
if (isset($_GET, $_GET['page'])) {
// sanitize the $_GET contents
switch ($_GET[['page']) {
case 'form' :
header( 'Location: sections/forum.php' );
exit;
break;
case '...' :
// etc etc
}
} else {
echo 'No $_GET';
}
Unfortunately it does not stop there, as its likely you will want to put other parameters on the querystring as well as the page. So now you have to decide what to do with those other parameters. Do you add them to the header() or do you store them somewhere else and make sure the rest of you app knows where to get them from.
<?php
if (isset($_GET, $_GET['page'])) {
// sanitize the $_GET contents
$gets = $_GET;
unset($gets['page'];
$qs = '?' . implode('&',$gets);
switch ($_GET[['page']) {
case 'form' :
header( 'Location: sections/forum.php' . $qs );
exit;
break;
case '...' :
// etc etc
}
} else {
echo 'No $_GET';
}
You have a lot of ways to do this, but if i understood your question, one solution could be:
if (!empty($_GET["page"])) {
switch ($_GET["page"]) {
case "forum":
include('section/forum.php');
break;
case "something_else":
//include other file, or do whatever want
break;
default:
//every else case
break;
}
}
Thanks both, yea kinda work's but in the browser instead of showing mysite.com/index.php?page=forum shows mysite.com/sections/forum.php
Did I missed something ?
Thanks.

Check result of PHP include

I've got my login and session validity functions all set up and running.
What I would like to do is include this file at the beginning of every page and based on the output of this file it would either present the desired information or, if the user is not logged in simply show the login form (which is an include).
How would I go about doing this? I wouldn't mind using an IF statement to test the output of the include but I've no idea how to go about getting this input.
Currently the login/session functions return true or false based on what happens.
Thanks.
EDIT: This is some of the code used in my login/session check but I would like my main file to basically know if the included file (the code below) has returned true of false.
if ($req_method == "POST"){
$uName = mysql_real_escape_string($_POST['uName']);
$pWD = mysql_real_escape_string($_POST['pWD']);
if (login($uName, $pWD, $db) == true){
echo "true"; //Login Sucessful
return true;
} else {
echo "false";
return false;
}
} else {
if (session_check($db) == true){
return true;
} else {
return false;
}
}
You could mean
if (include 'session_check.php') { echo "yeah it included ok"; }
or
logincheck.php'
if (some condition) $session_check=true;
else $session_check=false;
someotherpage.php
include 'session_check.php';
if ($session_check) { echo "yes it's true"; }
OR you could be expecting logincheck.php to run and echo "true" in which case you're doing it wrong.
EDIT:
Yes it was the latter. You can't return something from an included file, it's procedure not a function. Do this instead and see above
if (session_check($db) == true){
$session_check=true;
} else {
$session_check=false;
}
Actually..
$session_check=session_check($db);
is enough
Depending on where you want to check this, you may need to declare global $session_check; or you could set a constant instead.
you could have an included file which sets a variable:
<?php
$allOk = true;
and check for it in you main file:
<?php
include "included.php";
if ($allOk) {
echo "go on";
} else {
echo "There's an issue";
}
Your question seems to display some confusion about how php includes work, so I'm going to explain them a little and I think that'll solve your problem.
When you include something in PHP, it is exactly like running the code on that page without an include, just like if you copied and pasted. So you can do this:
includeme.php
$hello = 'world';
main.php
include 'includeme.php';
print $hello;
and that will print 'world'.
Unlike other languages, there is also no restriction about where an include file is placed in PHP. So you can do this too:
if ($whatever = true) {
include 'includeme.php';
}
Now both of these are considered 'bad code'. The first because you are using the global scope to pass information around and the second because you are running globally scoped stuff in an include on purpose.
For 'good' code, all included files should be classes and you should create a new instance of that class and do stuff, but that is a different discussion.

Not able to GET parameters functioning in include_once

when i call include once without any GET parameters it works but with setting GET parameters on trackinglogs.php nothing happens please suggest me what do to..
my php code is: firstfile.php
include_once('trackinglogs.php?todo=setcookie');
?>
my second file is trackinglogs.php
<?php
$action=$_GET['todo'];
switch($action)
{
case "setcookie":
echo "hi";die();
break;
default:
echo "error"; die();
break;
}
?>
thanks for you precious time
You cannot pass parameters when including like that, include does not make an HTTP request.
The most minimal solution, although I do not recommend it, is to simply set the parameters yourself so that trackinglogs.php finds them:
$_GET['todo'] = 'setcookie';
include_once('trackinglogs.php');
A much better solution would be to put the code that tracks logs inside a function, and call that providing this operating parameters at the same time. So you 'd have something like:
<?php
function track($action) {
switch($action) {
case "setcookie":
echo "hi";die();
break;
default:
echo "error"; die();
break;
}
And you would do:
include_once('trackinglogs.php');
track('setcookie');

Categories