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";
}
?>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am making a html for showing a "Problem Statement" of a contest. The problem has translation in 2 language English and Bengali. I can make this using 2 htmls. But how can I have like this -
desc.html?lang=en or desc.html?lang=bn or some other kind so that I can tell the page to show a specific language?
I am a newbie in web development.
Say, you have 2 files currently, index-english.html and index-german.html
You now create a file called index-global.php. Whether the first two pages are fully HTML or contain PHP, doesn't matter, but you should keep the current extension.Thanks to fred-ii for mentioning that you should change the extension to .php for the HTML-pages
In the index-global.php you'll place the following code
if (isset($_GET['lang']) AND $_GET['lang'] == "en") require("index-english.html");
elseif (isset($_GET['lang']) AND $_GET['lang'] == "de") require("index-german.html");
else {
header("Location: /?lang=en"); //redirect if no language specified
exit();
}
A better way would be to check the language automatically based on the Accept-Language-header, for example
$languages_supported_by_client = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($language_supported_by_client as $language) {
if($language == "en") {
require("index-english.html");
exit();
}
elseif($language == "de") {
require("index-german.html");
exit();
}
}
require("index-english.html");
exit();
In this case the asker wanted to include all HTML in one file
$languages_supported_by_client = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach ($language_supported_by_client as $language) {
if($language == "en") {
?>English HTML content here<?php
exit();
}
elseif($language == "de") {
?>German HTML content here<?php
exit();
}
}
?>English HTML content here<?php
exit();
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.
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.
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
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
I would like to set a javascript variable 'logged_in' to either 1 or 0, depending on whether a user is logged in Wordpress. This is what I have:
<script type="text/javascript">
var logged_in = <?php if ( is_user_logged_in() ) { echo '1';} else {echo '0';} ?>;
</script>
But it's not working. What's wrong with it?
I just checked your coding. Your code will define the variable you declared in your script as 1 if true and 0 if false. Keep in mind that you are only defining a variable and it will not do anything as it is. Here is what I have. Just take a look your source code.
<?php
function is_user_logged_in() {
return true;
}
?>
<script type="text/javascript">
var logged_in = <?php if ( is_user_logged_in() ) { echo '1';} else {echo '0';} ?>;
</script>
If it is not setting the variable make sure that the function is_user_logged_in() exist and is available on the script.