How to display pages with different header? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am working on website for my school project. But I came across with a problem. I display a header in every page. One of my header contains login form and other one contains username, search bar, etc..
The questions is, there are 3 pages; faq, contact, about. And I want to show them either user is logged in or not. But the headers are problem. I want user to see the header-after-login if user is logged in if not I wanna show header-before-login.
I have the code any everything. I need a way or a logic to fix this issue.
Thank you.

header();
function header(){
$header = "<div ";
if($_SESSION['userID']){
$header .= "class=\"header user_info_header\"";
$content = "logged user info";
}else{
$header .= "class=\"header login_form_header\"";
$content = "login form";
}
$header .= ">".$content."</div>";
echo $header;
}

There's probably a million and one ways to solve this, and I'm assuming you're able to programatically find out if the user is logged in.
faq.php, contact.php, about.php:
include 'header.php';
//content
include 'footer.php';
header.php:
echo '<!doctype html><html>...';
if($user_is_logged_in == true) {
echo '<body class="logged_in">You are logged in!';
include 'sidebar.php';
} else {
echo '<body class="not_logged_in">You are not logged in!';
include 'user_not_logged_in_header.php';
}
echo '<div class="main">';
footer.php:
echo '</div></body></html>';
sidebar.php:
echo '<div class="sidebar">Sidebar!</div>;
Then the sidebar div and main div can be independently controlled using the classes.

Related

Different Page View for User [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to try this. But I don't know that this will work or not and whether this is a good way for me to achieve what I want.
What I want is to show different page view for users (login and not login user)
I am going to use this method with session, but please check if I do it correctly:
<?php
session_start();
if ( $_SESSION['login_id'] == 0 || $_SESSION['login_id'] == '' ) {
echo "user is not login, and I will show the not login page view to them";
} else {
echo "user is login, and I will show the login page view to them";
}
require_once('configPDO.php');
?>
If you stored the variable in $_SESSION in the right way, you can redirect the user in different pages using header after the login.
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])){
header("location:PAGELOGIN.php");
}
else{
header("location:PAGENOTLOGIN.php");
}
?>
if you want to stay in the same page, you can do:
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id']))
{
?>
YOUR HTML CODE
<?
} else {
?> YOUR HTML CODE
<?}
?>
this will show your html code using the php condition. tell me if this work ^^
try this...
<?php
session_start();
if(isset($_SESSION['user'])){
echo "iam login";
}
else{
echo "not login";
}
?>

how to post SQL variable through session and get it in next page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to post $dataHeadArr value through session and get it in next page.
<?php
if(isset($_POST["search"]))
{ $storename=$_POST["StoreName"];
$dataHeadArr=$db->query("SELECT *FROM `opening_stk`");?>
On for the page you have posted put
<?php
if(isset($_POST["search"])){
session_start();
$storename=$_POST["StoreName"];
$dataHeadArr=$db->query("SELECT * FROM `opening_stk`");
while($row=mysql_fetch_assoc($dataHeadArr)){
$allRows[]=$row;
}
$_SESSION['dataHeadArr']=$allRows;
}
?>
For the page that you want to retreive the variable put
<?php
session_start();
$dataHeadArr=$_SESSION['dataHeadArr'];
//This mysql result been parsed already, so you should be
//able to access values when you loop through it from here
for($i=0;$i<count($dataHeadArr); $i++){
echo "Printing row ".$i."</br>";
foreach($dataHeadArr[$i] as $key=>$item){
//Prints out all value for the row
echo $item[$key]."</br>";
}
echo "</br>";
}
?>
You may just set it in the POST or SESSION array and then retrieve it on next page. For ex-
$_POST['dataHeadArr']=$dataHeadArr;
To store...
$_SESSION['dataHeadArr'] = 'Whatever';
Then to retrieve simply..
if(isset($_SESSION['dataHeadArr'])){
echo $_SESSION['dataHeadArr'];
}
Make sure to call session_start(); at the top of every page.

Creating custom ahref links in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do you create a custom links in ahref using php as shown below
link from : http://www.top10bestwebsitehosting.com/visit.php?site=iPage
goes to : http://www.ipage.com
Perhaps something like this:
$sites = array(
'iPage' => 'http://www.ipage.com/',
'Google' => 'http://www.google.com/',
);
$key = $_GET['site'];
if(isset($sites[$key])) {
header('Location: ' . $sites[$key]);
exit;
}
echo 'Sorry, no such site.';
There is redirect, if you open in firefox developer tools and after open a link, and pressing escape key(else you will be redirected) you can see the next url:
http://naturaltracking.com/redirect.php?url=http%3A%2F%2Fwww.ipage.com%2Fjoin%2Findex.bml%3FAffID%3D638751%26amp%3BLinkName%3D&uid=6344b6a239bb&sid=374eaa4bb6d2&hukc=1&nicid=00004ZQNza&nivkey=FiiRIhiDHa
It was most likely done in this way:
have an internal list of what $_GET['site'] can be
read $_GET['site']
redirect to the corresponding website
This is an example:
<?php
// visit.php
$pages = array('iPage'=>'www.ipage.com','google','www.google.com');
if (in_array($pages,$_GET['site']))
{
// do some internal logging or whatever
header("Location: ".$pages[$_GET['site']]);
}
?>
The way top10bestwebsitehosting does is, it keeps the urls in the database. So then it does something similar to the below:
$name = mysql_real_escape_string($_GET['site']); //check for escapes etc.
$query = mysql_query('select * from `websites` where `name` = {$name} limit 1');
$row = mysql_fetch_row($query);
header('Location: '.$row['url']);

Integrating Clean code when mixing PHP and HTML [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm finding an organisation a major problem with mixing PHP and HTML, it just looks horrible, so i'm wondering if it's a viable option to create a set of object oriented methods such as this:
class MainOO {
public $Database;
public function __construct($Server,$User, $Password, $DB){
if ($this->Database = new mysqli($Server,$User,$Password,$DB)){
return true;
}
return false;
}
public function User_Login(){
$Get_Usr_Info = $this->Database->prepare("SELECT ID, Password, Salt FROM Users WHERE Username=?");
$Get_Usr_Info->bind_param('s',$_POST['username']);
$Get_Usr_Info->execute();
$Get_Usr_Info->store_result();
$User_Number = $Get_Usr_Info->num_rows;
$Get_Usr_Info->bind_result($UserID, $Stored_Password, $Stored_Salt);
$Get_Usr_Info->fetch();
$Get_Usr_Info->close();
if ($User_Number !== 1){
$Error = "Wrong Username Specified Or Password Is Incorrect";
header ("Location: index.php?Errors=".urlencode($Error));
exit;
}
// Continue with login script
}
public function Logout(){
if (session_status() !== PHP_SESSION_DISABLED){
session_destroy();
header ("Location: LoggedOut.php");
exit;
}
}
}
Then HTML side:
<?php
include "MainOO.php";
$MainOO = new MainOO("host","user","password","database");
?>
<div class="example">
<div class="example left">
<?php
$MainOO->User_Login();
?>
</div>
</div>
It's still mixing PHP & HTML, but it's making look a hell of a lot neater than having heaps of PHP in the middle of HTML.
I'm fully aware I could migrate over to a MVC Framework (which this topic is looking like) already setup, or even use a template engine such as smarty, but I want to avoid this as much as possible.. So is this a viable option to have neater PHP code within html?
You will probably want to use an isset() in the code too
e.g
<?= (isset($variable)) ? $variable : ''; ?>
e.g if variable isset then display it otherwise display nothing

Trying to echo a variable ( $i) into another variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special form I have been making that uses some cusotm post types in wordpress. At one point I need to echo a variable $i into an if statement.
There is some validation stuff at the top that will look like this and the code in the loop is below. Pretty much I have been trying to get the majorCause1Error to be majorCause $i Error if you know what I mean, so all up it will be like 1-13
Edit: Sorry If it is hard to see what I am asking, I am finding it really hard to word my problem.
So there is a loop running around the li tags and it echos $i into the name etc so it becomes majorCause1 then next one majorCause2 and the next one magjorCause3 etc etc
Under the labels there is an if statement that is like - if($majorCause1Error !='') { do something } - I want this to be like if($majorCause1Error !=''){} and then the next one be like if($majorCause2Error !=''){} and then if($majorCause3Error !=''){}
Does this make more sense?
Here is a link to the site http://www.foresightaus.com.au/form/
if(trim($_POST['majorCause1']) === '') {
$majorCause1Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause1 = trim($_POST['majorCause1']);
}
if(trim($_POST['majorCause2']) === '') {
$majorCause2Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause2 = trim($_POST['majorCause2']);
}
<li class="fill-in">
<label for="majorCause<?php echo($i); ?>"><?php echo($j); ?>. State one major cause:</label>
<input type="text" name="majorCause<?php echo($i); ?>" id="majorCause<?php echo($i); ?>" value=""/>
<?php if($majorCause1Error != '') { ?>
<span class="error"><?=$majorCause1Error;?></span>
<?php } ?>
</li>
You probably want to be using an array but what you are referencing is called a variable variable and is supported by PHP!
Something like this should do it
${"majorCause{$i}Error"}

Categories