My $_SESSION don't work on my page why? - php

i am using $session[login] but $_SESSION seem like don't not work on my home page of html/php webpage . It don have show up anything . why? could anyone help me
and tell me why? is it the session name wrong? or need to input php inside?
<html>
<title>Home</title>
<head>
<link href="SSCdesign.css" rel="stylesheet" type="text/css">
<script src="script.js"> </script>
</head>
<body>
<header id="SSClogo"><img src="SSC_logo_v2.png"></header>
<div id='userbar'>
<?php
$_SESSION['login'] = true;
while($row = mysql_fetch_assoc($result))
{
$_SESSION['student_id'] = $row['studnet_id'];
$_SESSION['student_name'] = $row['student_name'];
}
?>
</div>
<div id='wrapper' align="center">
<nav id='cssmenu'>
<ul>
<li class='active'><a href='SSC.html'><span>Home</span></a></li>
<li><a href='advisors.html'><span>Advisors</span></a></li>
<li class='has-sub'><a href='pals.html'><span>PALS</span></a>
<ul>
<li><a href='registersession.html'><span>Register Session</span></a></li>
<li><a href='timetable.html'><span>Timetable</span></a></li>
<li><a href='feedback.html'><span>Feedback</span></a></li>
<li class='last'><a href='buzz.html'><span>Buzz</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='econsultation.html'><span>e-Consultation</span></a>
<ul>
<li><a href='appointment.html'><span>e-Appointment</span></a></li>
<li><a href='upcoming.html'><span>Upcoming List Appointment</span></a></li>
<li class='last'><a href='history.html'><span>History</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='workshop.html'><span>Workshop</span></a>
<ul>
<li><a href='calendar.html'><span>Calendar</span></a></li>
<li class='last'><a href='eportfolio.html'><span>e-Portfolio</span></a></li>
</ul>
</li>
<li><a href='facilities.html'><span>Facilities</span></a></li>
<li class='last'><a href='contactus.html'><span>Contact Us</span></a></li>
<li class='la'><a href='login.html'><span>Login</span></a></li>
</ul>
<div id="announcement">
<h2> <center> Announcement will be posted here </center> </h2>
<p> <center> This site is currently under development, do check it out soon! </center> </p>
</div>
<div id="slideshow">
<h2> <center> Slideshow will be here </center> </h2>
</div>
</nav>
<div class="push"></div>
</div>
</body>
</html>

You have to start your session before you use it with:
session_start();
So your code should looks something like this:
<?php
session_start();
$_SESSION['login'] = true;
(Also if you say you use: $session[login], it's wrong you would have to use it with: $session['login'] otherwise login is a constant which it isn't)
For more information see: http://php.net/manual/en/function.session-start.php

Related

When session starts header file changes after refreshing the page

New to Codeigniter.
This is my header file. After getting logged in, the header file needs to be changed but it changes after I refresh the page. Same is when I logout. Kindly help.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li><a href=<?php echo base_url( 'form/home')?>>HOME</a></li>
<li><a href=<?php echo base_url( 'form/about')?>>ABOUT</a> </li>
<li><a href=<?php echo base_url( 'form/contact')?>>CONTACT</a></li>
</ul>
<?php
if(isset($_SESSION["name"])){
?>
<ul class="nav navbar-nav navbar-right">
<li><a href=<?php echo base_url( 'form/welcome')?>><?php echo $_SESSION["name"];?></a></li>
<li></span></li>
</ul>
<?php
}else{
?>
<ul class="nav navbar-nav navbar-right">
<li></span></li>
<li></span> </li>
</ul>
<?php
}
?>
</div>
</nav>
</body>
</html>
Change the if condition inorder to create session with a name.
if($query -> num_rows() == 1)
{
$row = $query->row();
$data = array('name' => $row->name);
$this->session->set_userdata('loggin_session',$data); // given a name to session(i.e loggin_session).
return true;
}
Now in your View change like following:
<?php
$session_data = $this->session->userdata('loggin_session');
if(isset($session_data['name'])){
?>
<ul class="nav navbar-nav navbar-right">
<li><a href=<?php echo base_url('form/welcome')?>><?php echo $_SESSION["name"];?></a></li>
<li></span></li>
</ul>
<?php }else{ ?>
<ul class="nav navbar-nav navbar-right">
<li></span></li>
<li></span> </li>
</ul>
<?php } ?>

PHP Menu item breaks all following menu items

The menus are all separated into PHP includes and called in the main index.php file. The issue is all of the menu items work fine until a specific menu item. At which point that linked page and all of the following links after it in the menu stop functioning without any sort of inspector error. The problem page is titled eva.php. From what I can tell the original creator of the site has all of the pages are being called correctly, but I'm fairly new to PHP so forgive any ignorance on my part.
Index File:
<!doctype html>
<html lang="en">
<head>
<?php require_once('inc/meta.php'); ?>
<?php require_once('inc/script.php'); ?>
<?php require_once('inc/styles.php'); ?>
</head>
<body>
<div id="slidemenu" class="scrollable closed">
<ul id="menuAll">
<h2>Applications</h2>
<li>Furniture, Cabinet & Countertop Substrates</li>
<li>Engineered Flooring Substrates</li>
<li>Flooring, Furniture, Cabinet & Countertop Laminates</li>
<li>Molding & Millwork</li>
<li>Doors & Windows</li>
<li>Framing</li>
<li>Sheathing</li>
<li>Roofing</li>
<li>Insulation</li>
<li>Decks, Porches, Cladding & Fences</li>
<h2>More</h2>
<li>Sustainability</li>
<li>Formaldehyde in Construction Video</li>
</ul>
<?php require_once('inc/menu-framing.php'); ?>
<?php require_once('inc/menu-cabinets-counters.php'); ?>
<?php require_once('inc/menu-flooring-furniture-cabinets-counters.php'); ?>
<?php require_once('inc/menu-doors-windows.php'); ?>
<?php require_once('inc/menu-furniture.php'); ?>
<?php require_once('inc/menu-sheathing.php'); ?>
<?php require_once('inc/menu-roofing.php'); ?>
<?php require_once('inc/menu-insulation.php'); ?>
<?php require_once('inc/menu-moulding-millwork.php'); ?>
<?php require_once('inc/menu-engineered-flooring.php'); ?>
<?php require_once('inc/menu-locations.php'); ?>
<?php require_once('inc/menu-decks.php'); ?>
</div>
<div data-role="page" id="main_page" data-theme="a">
<div data-role="header"> ☰
</div>
</div>
<?php require_once('inc/page-other.php'); ?>
<?php require_once('inc/page-urea-formaldehyde.php'); ?>
<?php require_once('inc/page-melamine-formaldehyde-powder.php'); ?>
<?php require_once('inc/page-melamine-urea-formaldehyde.php'); ?>
<?php require_once('inc/page-resorcinol-formaldehyde.php'); ?>
<?php require_once('inc/page-phenol-resorcinol-formaldehyde.php'); ?>
<?php require_once('inc/page-emulsion-polymerized-isocyanate.php'); ?>
<?php require_once('inc/page-polyvinyl-acetate.php'); ?>
<?php require_once('inc/page-polyurethane-dispersions.php'); ?>
<?php require_once('inc/page-hot-melts.php'); ?>
<?php require_once('inc/page-casein-glues.php'); ?>
<?php require_once('inc/page-phenol-formaldehyde.php'); ?>
<?php require_once('inc/page-phenol-formaldehyde-adhesive.php'); ?>
<?php require_once('inc/page-phenol-formaldehyde-powder.php'); ?>
<?php require_once('inc/page-phenol-resorcinol-based-resins.php'); ?>
<?php require_once('inc/page-wax-emulsions.php'); ?>
<?php require_once('inc/page-melamine-urea-phenol-formaldehyde.php'); ?>
<?php require_once('inc/page-pva-adhesives.php'); ?>
<?php require_once('inc/page-melamine-based-resins.php'); ?>
<?php require_once('inc/page-melamine-resorcinol-based-resins.php'); ?>
<?php require_once('inc/page-resorcinol-based-resins.php'); ?>
<?php require_once('inc/page-fentak.php'); ?>
<?php require_once('inc/page-osb-specialized.php'); ?>
<?php require_once('inc/page-phenolic-resins.php'); ?>
<?php require_once('inc/page-amino-resins.php'); ?>
<?php require_once('inc/page-wood.php'); ?>
<?php require_once('inc/page-eva.php'); ?> <-- PROBLEM PAGE
<?php require_once('inc/page-lvl-ready.php'); ?>
<?php require_once('inc/page-lvl-reactor.php'); ?>
<?php require_once('inc/page-lvl-in-plant.php'); ?>
<?php require_once('inc/page-sustainability.php'); ?>
<?php require_once('inc/page-video1.php'); ?>
<?php require_once('inc/page-video2.php'); ?>
<?php require_once('inc/page-na.php'); ?>
<?php require_once('inc/page-sa.php'); ?>
<?php require_once('inc/page-apac.php'); ?>
<?php require_once('inc/page-europe.php'); ?>
</body>
</html>
Menu Include:
<ul id="menuFlooringFurnitureCabinetsCounters">
<h2>Flooring, Furniture, Cabinet & Countertop Laminates</h2>
<li><a href="#">
<img src="img/thumb/particleboard.jpg" width="70" height="70" />
<h3>Countertop Laminates</h3></a>
<ul>
<h4>Countertop Laminates</h4>
<li><a data-prefetch href="#page-urea-formaldehyde">Urea Formaldehyde Resins</a></li>
<li><a data-prefetch href="#page-melamine-formaldehyde-powder">Melamine Formaldehyde Resins</a></li>
<li><a data-prefetch href="#page-melamine-urea-formaldehyde">Melamine-Urea Formaldehyde Resins</a></li>
<li><a data-prefetch href="#page-polyvinyl-acetate">Polyvinyl Acetate Adhesives</a></li>
<li><a data-prefetch href="#page-eva">Ethylene Vinyl Acetate Adhesives</a></li>
<li><a data-prefetch href="#page-fentak">Fentak Additives</a></li>
</ul>
</li>
<li><a href="#">
<img src="img/thumb/mdf.jpg" width="70" height="70" />
<h3>Cabinet Laminates</h3></a>
<ul>
<h4>Cabinet Laminates</h4>
<li><a data-prefetch href="#page-fentak">Fentak Additives</a></li>
<li><a data-prefetch href="#page-urea-formaldehyde">Urea Formaldehyde Resins</a></li>
<li><a data-prefetch href="#page-melamine-urea-formaldehyde">Melamine-urea Formaldehyde Resins</a></li>
<li><a data-prefetch href="#page-melamine-formaldehyde-powder">Melamine Formaldehyde Resins</a></li>
<li><a data-prefetch href="#page-polyvinyl-acetate">Polyvinyl Acetate Adhesives</a></li>
<li><a data-prefetch href="#page-eva">Ethylene Vinyl Acetate Adhesives</a></li>
</ul>
</li>
<li><a href="#">
<img src="img/thumb/plywood.jpg" width="70" height="70" />
<h3>Flooring Laminates</h3></a>
<ul>
<h4>Flooring Laminates</h4>
<li><a data-prefetch href="#page-fentak">Fentak Additives</a></li>
<li><a data-prefetch href="#page-urea-formaldehyde">Urea Formaldehyde Resins</a></li>
<li><a data-prefetch href="#page-melamine-urea-formaldehyde">Melamine-urea Formaldehyde Resins</a></li>
<li><a data-prefetch href="#page-melamine-formaldehyde-powder">Melamine Formaldehyde Resins</a></li>
<li><a data-prefetch href="#page-pva-adhesives">Polyvinyl Acetate Adhesives</a></li>
</ul>
</li>
</ul>
Eva Page:
<div data-role="page" id="page-eva" data-theme="a" data-cache="never">
<div data-role="header"> ☰
</div>
<div data-role="content" id="eva-ethylene-vinyl-acetate">
<div class="form"></div><div class="pad">
</div>
</div>

show active menu while user on that menu and change when menu is changed

I am currently working on a website with this template:
<!DOCTYPE html>
<html lang="en" >
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<link rel="icon" type="image/gif/png" href="">
<meta charset="utf-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="" media="all">
</head>
<body>
<div class="wrapper">
<div class="banner">
</div>
<div class="nav_bar">
<ul>
<li><a class="home" href="">Home</a></li>
<li><a class="news" href="">News</a></li>
<li><a class="r_faqs" href="">Registration FAQs</a></li>
<li><a class="howto" href="">How to Register and Rules</a></li>
<li><a class="sch_reg" href="">Register school</a></li>
<li><a class="p_reg" href="">Register pupil</a></li>
<li><a class="about" href="">About Us</a></li>
<li><a class="contact" href="">Contact Us</a></li>
</ul>
</div>
<div class="content_area">
<?php echo $content; ?>
</div>
<div class="clear"></div>
</div>
<div class="footer">
<div>
</div>
</div>
</body>
</html>
I have styled the navigation vertically with the content on its right side like in this page http://learn.shayhowe.com/advanced-html-css/complex-selectors/
Now the question is how do i keep the active menu highlighted while user is on that page. you can view my page here https://jsfiddle.net/96wxdzk7/3/
<ul class="sub-nav" >
<?php
$full_name = $_SERVER['PHP_SELF'];
$name_array = explode('/',$full_name);
$count = count($name_array);
$page_name = $name_array[$count-1];
?>
<li><a class="<?php echo ($page_name=='where-to-buy.php')?'active':'';?>" href="where-to-buy.php">WHERE TO BUY</a></li>
<li><a class="<?php echo ($page_name=='about.php')?'active':'';?>" href="about.php">ABOUT US</a></li>
<li><a class="<?php echo ($page_name=='contact.php')?'active':'';?>" href="contact.php">CONTACT US</a></li>
Also follow below URL for live demo for this..
https://webdesignerhut.com/active-class-navigation-menu/

How do I set an 'active' class to a menu item using PHP?

I wanted to set class="active" for the current active page but I couldn't get it to work. I keep getting the same error each time I included <?php echo ($page == 'home') ? 'class="current"' : '';?> in my anchor and listing tags:
Parse error: syntax error, unexpected 'home' (T_STRING), expecting ',' or ';' in C:\Users\Standard\Desktop\Test\includes\top.php on line 18
I just don't know what I'm doing wrong at the moment... I been researching and searching for the answer for the past 3 hours but still can't solve it. I just keep getting syntax errors. :(
This is my current code:
<?php
echo '
<div class="top-nav-bg">
<div class="top-nav container">
<div class="row">
<div class="top-nav sixteen columns">
<ul>
<li><a <?php echo ($page == 'home') ? 'class="active"' : '';?> href="/index.php">Home</a></li>
<li>About Us</li>
<li>Our Services</li>
<li>Products</li>
<li>Contact Us</li>
<ul>
</div>
<div>
</div>
</div>
' ; >?
It's being included in my index.php file:
<!DOCTYPE html>
<html lang="en">
<head>
<?php include ('/includes/header.php'); ?>
<title>Home - Test</title>
</head>
<body id="homepage">
<?php $page = 'home'; include ('/includes/top.php') ;?>
</body>
</html>
you're already inside PHP, so your <?php echo isn't necessary. should just be:
<?php
echo '
<div class="top-nav-bg">
<div class="top-nav container">
<div class="row">
<div class="top-nav sixteen columns">
<ul>
<li><a '.($page == 'home') ? 'class="active"' : ''.' href="/index.php">Home</a></li>
<li>About Us</li>
<li>Our Services</li>
<li>Products</li>
<li>Contact Us</li>
<ul>
</div>
<div>
</div>
</div>
' ;
?>
Don't have rep to comment so will answer.
You're only outputing one variable so you don't need to have all that html inside php.
Plus by taking it out you won't have to worry about striping ' or ".
Also if you're using a nice IDE/editor, you get better syntax highlighting.
Here's how you can do it in a cleaner way:
<div class="top-nav-bg">
<div class="top-nav container">
<div class="row">
<div class="top-nav sixteen columns">
<ul>
<li><a <?= ($page === 'home') ? 'class="active"' : ''; ?> href="/index.php">Home</a></li>
<li>About Us</li>
<li>Our Services</li>
<li>Products</li>
<li>Contact Us</li>
<ul>
</div>
<div>
</div>
</div>
Notice the use of <?=, this is shorthand for <?php echo.
Only php 5.4+ will accept them so use <?php echo if you're unsure it may be a problem.

target href in section MAIN

I am developing a web application and I am having a problem with a menu in the header.
I explain better:
my page is divided like this:
<div>
<header>
<font color=white align='center'>HEADER</font>
<nav class='clearfix'>
<ul class='clearfix'>
<li><a id='home' href='#'>Home</a></li>
<li><a id='pagina1' href='#'>pagina1</a></li>
<li><a id='pagina2' href='#'>pagina2</a></li>
</ul>
<a href='#' id='pull'>Menu</a>
</nav>
</header>
</div>
<main>
<h1> Main</h1>
</main>
<footer>
<nav class='clearfix'>
<font color=white align='center'>FOOTER</font>
</nav>
</footer>
And obviously I have other pages like pagina1.php and pagina2.php
Now....I would like to load the pagina1.php in the when I click on the button on the menu.
I tried some scripts but it seems not not working...Can someone help me?
You have not specified a location for the link to go to. href='#' will keep you on the same page. Change this to any other url.
for example
<li><a id='home' href='/home.php'>Home</a></li>
<li><a id='pagina1' href='/pagina1.php'>/pagina1</a></li>
<li><a id='pagina2' href='/pagina2.php'>/pagina2/a></li>
EDIT:
Since your recent comment indicates you want to change the content of main when you click on a link, this should do the trick.
Create a page called pagina1.html (or php, does not matter) and add the following content:
Pagina1
Create a page called pagina2.html (or php, does not matter) and add the following content:
Pagina 2
Modify your existing page to:
<!DOCTYPE html>
<html>
<head>
<script data-require="jquery" data-semver="2.0.3" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(function () {
$("#pagina1").click(function(){
$.ajax({url:"pagina1.html",success:function(result){
$("#main").html(result);
}});
});
$("#pagina2").click(function(){
$.ajax({url:"pagina2.html",success:function(result){
$("#main").html(result);
}});
});
});
</script>
</head>
<div>
<header>
<font color=white align='center'>HEADER</font>
<nav class='clearfix'>
<ul class='clearfix'>
<li><a id='home' href='#'>Home</a></li>
<li><a id='pagina1' href='#'>pagina1</a></li>
<li><a id='pagina2' href='#'>pagina2</a></li>
</ul>
<a href='#' id='pull'>Menu</a>
</nav>
</header>
</div>
<div id="main">
<h1> Main</h1>
</div>
<footer>
<nav class='clearfix'>
<font color=white align='center'>FOOTER</font>
</nav>
</footer>
I've created a plunk where you can see the code working:
http://plnkr.co/edit/iJZSbWYCWGH3NKBzwY7u?p=preview
you need ajax,
something like this, see http://www.w3schools.com/jquery/ajax_ajax.asp
$("#pagina1, #pagina2").click(function(){
page = $(this).attr("id");
$.ajax({url: page + ".php", success: function(result){
$("main").html(result);
}});
});
Simply use load function
$("#pagina1").click(function(){
$("main").empty();
$("main").load(pagina.php);
});
$("#pagina2").click(function(){
$("main").empty();
$("main").load(pagina2.php);
});
html
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<div>
<header>
<font color=white align='center'>HEADER</font>
<nav class='clearfix'>
<ul class='clearfix'>
<li id='home'>Home</a></li>
<li id='pagina1'>pagina1</a></li>
<li id='pagina2'>pagina2</a></li>
</ul>
<a href='#' id='pull'>Menu</a>
</nav>
</header>
</div>
<main>
<h1> Main</h1>
</main>
<footer>
<nav class='clearfix'>
<font color=white align='center'>FOOTER</font>
</nav>
</footer>

Categories