setting a value for index.php page - php

is there a way set a value for the index.php page on its first load?
i would like index.php it to load like so "index.php?subject=1" when it loads for the first time.
as this value will change as they move around in the site i don't want it to be a fixed value.
some one sugested
if(empty($_SESSION['visited'])) {
//DO STUFF
$_SESSION['visited'] = true; }
i cant seem to get that to work with my function.
find_selected_page()
function find_selected_page () {
global $current_subject;
global $current_page;
if (isset($_GET["subject"])) {
$current_subject = find_subject_by_id ($_GET["subject"]);
$current_page = find_default_post($current_subject ["id"]);
} elseif (isset($_GET["page"])) {
$current_subject = null;
$current_page = find_page_by_id ($_GET["page"]);
} else {
$current_page = null;
$current_subject = null;
}
}
index.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/session/session.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/db/dbcon.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/functions.php');
$context = "public";
find_selected_page ();
?>
<html>
<head>
<title>Home</title>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/style.php'); ?>
<body>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/header.php'); ?>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/nav_ribbon.php');?>
<div id="p2dbg">
<div id="p2dcontent">
<div class="p2dcontent">
<h1><?php echo htmlentities($current_subject ["menu_name"]); ?></h1><br />
<?php if ($current_page) { ?>
<p><?php echo nl2br($current_page ["content"]); ?></p><br />
<?php } else { ?>
This page does not exist! please slect a page from the menu.
<?php } ?>
</div>
</div>
<?php include($_SERVER['DOCUMENT_ROOT'].'/inc/footer.php'); ?>
</div>
</body>
</html>

Instead of setting everything to null if your value isn't passed, just assume the default value:
// Set this to the value you want as your default
define("DEFAULTSUBJECT",1);
function find_selected_page () {
global $current_subject;
global $current_page;
if (isset($_GET["subject"])) {
$current_subject = find_subject_by_id ($_GET["subject"]);
$current_page = find_default_post($current_subject ["id"]);
} elseif (isset($_GET["page"])) {
$current_subject = null;
$current_page = find_page_by_id ($_GET["page"]);
} else {
$current_subject = find_subject_by_id (DEFAULTSUBJECT);
$current_page = find_default_post($current_subject ["id"]);
}
}

Related

How to save favorite images after logout and still see them after login?

Problem:
So when I login, a user can go to the browse.php to add images to the favorites.php, and the photos will appear on that page.
When I log out and log back in, the images are gone (not saved).
Question:
How do I save it after I logout so on next login I get to see the favorite'd images from that particular user using cookies or other methods (can't use database because I'm not allowed to)?
My favorites page
<?php
session_start();
require_once 'favoritesfunction.php';
if (isset($_GET["fileName"])) {
$fileName = $_GET["fileName"];
addToFavorites($fileName);
}
?>
<!DOCTYPE html>
<html>
<head>
<?php
$title = "Travel Photos - View Favorites";
?>
<link rel="stylesheet" href="viewfavorites.css" />
</head>
<body>
<main class="container">
<h1 class="favheader">Favorites</h1>
<div class="content">
<?php
if (isset($_SESSION['favorites']) && isset($_SESSION['email'])) {
$favorites = $_SESSION['favorites'];
if (count($favorites) > 0) {
?> <ul class="ul-favorite"> <?php
foreach ($favorites as $f) {
echo '<img class="displayPic" src="https://storage.googleapis.com/assignment1_web2/square150/' . $f . '">';
}
?>
</ul>
<p id="removeall"><button class="button"><span><a target="" href="services/removefavorites.php?remove=all">Remove All</a></span></button></p>
<?php
}
} else {
?>
<div class="nofavorites">
<p>No favorites found.</p>
</div>
<?php
}
?>
</div>
</main>
</body>
<script type="text/javascript" src="main.js"></script>
</html>
My add to favorites function php
<?php
function addToFavorites($fileName)
{
//Checks if the session favorites exists
if (isset($_SESSION['favorites'])) {
$favorites = $_SESSION['favorites'];
} else {
$favorites = array();
$_SESSION['favorites'] = $favorites;
}
// add item to favourites
$item = $fileName;
$match = false;
//Loop below checks for duplicates
$i = 0;
while ($i < count($favorites)) {
if ($favorites[$i] == $item) {
$favorites[$i] = $item;
$match = true;
}
$i++;
}
//if match equals false, that means its not in the favorites list of the user
//so it is added to the user's favorites array
if ($match == false) {
$favorites[] = $item;
}
$_SESSION['favorites'] = $favorites;
$_SESSION['favorites'] = $favorites;
}
My approach:
I tried doing something like setting $name = $_SESSION['email'] and $value="<img src=...>" then setcookie($name, $value) and then if(isset($_COOKIE[$value])) echo $value
I know this is totally wrong, I tried looking everywhere online to try to solve this, if I could get some help that would be great thanks!

Best way for passing string while including file

I'm having a trouble with passing string while using include() function. I have index.php which serves as template file and is including any other pages.
For example, if I want to go to the home page, the address is http://somethingsomething.com/index.php?page=home and included file is pages/home.php. Bellow you can find how the template system works. I want to change of the page you're currently are but I don't know what is the best way to pass that string from the title of the page. So if I'm on the Catalogue page - http://somethingsomething.com/index.php?page=catalogue, it will include file pages/catalogue.php and I've tried to pass string as SESSION but it didn't worked. Could you please help me?
Source URL: http://somethingsomething.com/index.php?page=catalogue
index.php file
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome, world!</h1>
<?php
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 'home';
}
if (preg_match('/^[a-z0-9]+$/', $page)) {
$insert = include('pages/' . $page . '.php');
if (!$insert) {
$insert = include('pages/error.php');
}
} else if (empty($page)) {
$insert = include('pages/error.php');
} else {
$insert = include('pages/error.php');
}
?>
</body>
</html>
pages/catalogue.php file
<?php
$title = "Catalogue";
?>
Something like this?
<?php
ob_start();
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 'home';
}
if (preg_match('/^[a-z0-9]+$/', $page)) {
$insert = include('pages/' . $page . '.php');
if (!$insert) {
$insert = include('pages/error.php');
}
} else if (empty($page)) {
$insert = include('pages/error.php');
} else {
$insert = include('pages/error.php');
}
$output = ob_get_clean();
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome, world!</h1>
<?php
echo $output;
?>
</body>
</html>

PHP omit an include from specific page

I have a PHP site that includes a header (menu nav and logo).
I have a section in my site that I would like to include a DIFFERENT header
I am struggling with this. Any help would be appreciated!
Here is my code:
<div id="wrapper">
<? include('inc/header.inc.php'); ?>
<? require('process/build_page.php'); ?>
<? require('inc/footer.inc.php'); ?>
</div>
I want to include the following header on a specific section called "my-association"
<? include('inc/header_ma.inc.php'); ?>
The process/build_page code looks like this:
<?
global $full_uri;
class BuildPage
{
function BuildPage()
{
global $full_uri;
if($full_uri)
{
$this->build_path();
}
else
{
exit();
}
}
function build_path()
{
global $full_uri;
$uri_array = explode("/", $full_uri); # make uri into an array divided by /
$clean_uri = array_filter($uri_array); # remove empty elements
unset($clean_uri[1]); # remove base part of uri
$clean_uri = array_values($clean_uri); #reset array key to start with 0
# if array is empty the homepage is being requested
if(empty($clean_uri)){
$clean_uri[0] = 'home';
}
# build uri to point to include file
$new_path = "content/";
$new_path .= implode("/", $clean_uri);
$new_path .= ".inc.php";
$this->build($new_path);
}
function build($path)
{
$output = "";
$output .= $this->get_include_contents($path);
echo $output;
}
function get_include_contents($filename)
{
if (is_file($filename))
{
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
function print_array($data)
{
echo "<pre>";
print_r($data);
echo "</pre>";
die();
}
};
$buildpage = new BuildPage();
?>
Please advise!
Thanks
Please let me know if I've missed something but wouldn't this do the trick?
<div id="wrapper">
<?
$change_header = array('/special_page_1','/special_page_2','/special_page_3');
if(in_array($full_uri,$change_header))
include('inc/header_ma.inc.php');
else
include('inc/header.inc.php');
?>
<? require('process/build_page.php'); ?>
<? require('inc/footer.inc.php'); ?>
</div>

add live ID to BODY with PHP

I'm using the php code:
<body id="<?php echo str_replace("index.php?","",(basename($_SERVER['REQUEST_URI'],".php"))); ?>">
and
<?php
if(isset($_GET['start'])){
include('includes/start.php');
}else if(isset($_GET['help'])){
include('includes/help.php');
}else{
include('includes/start.php');
}
?>
It works great - cutting index.php?help to "help" and index.php?start to "start" in body ID. But when I enter index.php in body ID is "index" not "start". Is there any way to tell index.php with included start.php to display "start" id body ID?
UPDATE
It should work dynamically - body ID is name of included .php file, something like this code:
<?php
$page = str_replace(array( 'server_name', 'index', '?', '/', '.php'), '', $_SERVER['REQUEST_URI']);
$page = $page ? $page : 'start';
?>
<body id="<?php echo $page ?>">
Am not sure i get you 100% but you can try
$id = $_SERVER['QUERY_STRING'];
$bodyID = $id;
switch ($id) {
case "help" :
include ('includes/help.php');
break;
default :
$bodyID = "start";
include ('includes/start.php');
break;
}
printf("<body id=\"%s\" >", $bodyID);
If you run index.php?help it would include include ('includes/help.php'); and also output
<body id="help" >
It would be best to use this instead:
<?php
$ids = 'start,help,something';
$ids = explode(',',$ids);
$included = 0;
foreach ($ids as $id) {
if (isset($_GET[$id])) {
include('includes/'.$id.'.php');
$included = 1;
break;
}
}
if (!$included) include('includes/'.$ids[0].'.php');
?>
Then:
<body id="<?php echo $id; ?>">

Different page titles with one header file?

How do you do multiple page titles with on header file? Theres one thing though. For the index page, i've got
error_reporting(0);
if ($_GET["error"]=="404") {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
So i would have the header before anything else. So how would i manage to make so that
index?error=404 and index have different titles? Thanks in advance.
In overall_header.php
<?php
$title = "Hello, wolrd!";
if ( $_GET["error"] == "404" ) {
$title = "Error";
}
?>
<title><?php echo $title; ?></title>
Use JavaScript and document.title.
Example:
<script language="javascript">document.title = "My Title"</script>
JS can be used in body.
Another method is to set a $GLOBAL variable before including everything.
Example:
error_reporting(0);
$GLOBALS['404'] = 0;
if ($_GET["error"]=="404") {
$GLOBALS['404'] = 1;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/404");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
} else {
$GLOBALS['404'] = 0;
include("forum/styles/art_air/web_template/overall_header.php");
include("include/index");
include("forum/styles/art_air/web_template/overall_footer.php");
}
In your overall_header.php:
if($GLOBALS['404'] == 1) echo '<title>404: Not Found</title>';
else echo '<title>My Title</title>';
You could try a switch
<?php
$page = $_SERVER['PHP_SELF'];
switch($page) {
case "index.php":
echo "<title>My Homepage</title>";
break;
case "apples.php":
echo "<title>The Best Apples!</title>";
break;
case "bananas.php":
echo "<title>We Sell Bananas</title>";
break;
}
?>

Categories