HY!
i have an issue in php session. the issue is that the session work fine in index page but the username can't show in other pages.

lohin.php code

<?php session_start();
 include './header.php'; 
include 'connection.php';
if(isset($_POST) && count($_POST)>0) {
    $user = $_POST['username'];
    $pass = $_POST['password'];

    $sSQL = "SELECT * FROM ulog WHERE User_Name ='".$user."' AND Password = '".$pass."'";

    $result = mysql_query($sSQL) or die(mysql_error());
    $row=mysql_num_rows($result);
    if($row==1)
        {
        // Set username session variable
            $_SESSION['username'] = $_POST['username'];
            // Jump to secured page
            header('Location:index.php');
                }
}

    else
    {
        header("location:login.html");
    }



?>

index.php code

<?php session_start();
include './header.php'; 
    // Check, if username session is NOT set then this page will jump to login page
    if (!isset($_SESSION['username']))
        {
            header('Location:login.html');
        } 
 ?>
 <div id="welcome">
 <h4> Welcome <?php echo $_SESSION['username']; ?> <a href="logout.php">Logout</h4></a></p>
</div>
<div id="content">
<p>Learn to Design and Develop Website.<br />
 Learn Programing From Tutorial's teaches you the fundamentals of web development and not just programming. You will learn how to create amazing websites through programming and design tutorials. The web development tutorials on your left are designed for you to move through them in order to have an overall understanding of web design and development.</p>
</div>
<?php include './footer.php'; ?>

the code i use in other pages which is not working is..

<?php session_start();
include 'login.php';    
    // Check, if username session is NOT set then this page will jump to login page
    if (!isset($_SESSION['username']))
        {
            header('Location:login.html');
        } 
 ?>

the above code is used in other pages but it is not working..
what type of code i need to access username in all pages..

Recommended Answers

All 21 Replies

You are trying to use sessions with a html-file.
create your login form (now in login.html) in the login.php
And don't include it on other pages

In your 3rd block of code (Common for all other pages), you have started a session, then you included the login.php where you started the session again. Remove the duplication by deleting session_start in your 3rd block.

can any one add there sugistion with my code, through code i can clearly understand...

Why are you including the login.php page ?

<?php session_start();
    // Check, if username session is NOT set then this page will jump to login page
    if (!isset($_SESSION['username']))
        {
            header('Location:login.html');
        } 
 ?>

I can't see why you need to include login.php.
At first I suggested you remove the session_start() from this file because it was already written in login.php , but now I realized that login.php has no useful information for this file. Why are you including it ?

hmmm. i just tried it. with login.php or without it will not working Dear...

Can you put one of the other files that includes:

<?php session_start();
    // Check, if username session is NOT set then this page will jump to login page
    if (!isset($_SESSION['username']))
        {
            header('Location:login.html');
        } 
 ?>

the other file in which i want to show the logedin username is..

<?php session_start();   
    // Check, if username session is NOT set then this page will jump to login page
    if (!isset($_SESSION['username']))
        {
            header('Location:login.html');
        } 
 ?>

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
<title>Information</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="javascript/subscribe.js"></script>
</head>
<body>
<header class="head"><h1 class="h">LEARN PROGRAMMING FROM TUTORIAL'S</h1>
    <div id="login"><a href="login.html">Login</a><br>OR<br><a href="signup.html">Register</a>
</div>
<ul class="main">
<li><a href="index.php">HOME</a></li>
<li><a href="tutorial.html">TUTORIAL's</a></li>
<li><a href="information.html" class="select">INFORMATION</a></li>
<li><a href="about.html" >ABOUT US</a></li>
<li><a href="contact.html">CONTACT US</a></li>
</ul>
</header>
<div id="content">
<p id="info">For tools and platforms, the public can use many open source systems to aid in web development. A popular example, the LAMP (Linux, Apache, MySQL, PHP) stack is available for download online free of charge. This has kept the cost of learning web development to a minimum. Another contributing factor to the growth of the industry has been the rise of easy-to-use WYSIWYG web-development software, most prominently Adobe Dreamweaver, WebDev, and Microsoft Expression Studio. Using such software, virtually anyone can relatively quickly learn to develop a very basic web page. Knowledge of HyperText Markup Language (HTML) or of programming languages is still required to use such software, but the basics can be learned and implemented quickly with the help of help files, technical books, internet tutorials, or face-to-face training.
An ever growing set of tools and technologies have helped developers build more dynamic and interactive websites. Web developers now help to deliver applications as web services which were traditionally only available as applications on a desk-based computer.
Instead of running executable code on a local computer, users can interact with online applications to create new content. This has created new methods in communication[citation needed] and allowed for many opportunities to decentralize information and media distribution. Users can interact with applications from many locations, instead of being tied to a specific workstation for their application environment.
Examples of dramatic transformation in communication and commerce led by web development include e-commerce. Online auction-sites such as eBay have changed the way consumers find and purchase goods and services. Online retailers such as Amazon.com and Buy.com (among many others) have transformed the shopping and bargain-hunting experience for many consumers. Another good example of transformative communication led by web development is the blog. Web applications such as WordPress and Movable Type have created easily-implemented blog-environments for individual web sites. The popularity of open-source content management systems such as Joomla!, Drupal, XOOPS, and TYPO3 and enterprise content management systems such as Alfresco have extended web development's impact at online interaction and communication.
Web development has also impacted personal networking and marketing. Websites are no longer simply tools for work or for commerce, but serve more broadly for communication and social networking. Websites such as Facebook and Twitter provide users with a platform to communicate and organizations with a more personal and interactive way to engage the public.
<br>
<h3>Typical areas</h3><br>
Web Development can be split into many areas and a typical and basic web development hierarchy might consist of:
Client side coding[edit]
Ajax Asynchronous JavaScript provides new methods of using JavaScript, and other languages to improve the user experience.
Flash Adobe Flash Player is a ubiquitous browser plugin ready for RIAs. Flex 2 is also deployed to the Flash Player (version 9+).
JavaScript JavaScript is a ubiquitous client side platform for creating and delivering rich web applications that can also run across a wide variety of devices. It is a dialect of the scripting language ECMAScript.
jQuery Cross-browser JavaScript library designed to simplify and speed up the client-side scripting of HTML.
Microsoft Silverlight Microsoft's browser plugin that enables animation, vector graphics and high-definition video playback, programmed using XAML and .NET programming languages.
HTML5 and CSS3 Latest HTML proposed standard combined with the latest proposed standard for CSS natively supports much of the client-side functionality provided by other frameworks such as Flash and Silverlight
Looking at these items from an "umbrella approach", client side coding such as XHTML is executed and stored on a local client (in a web browser) whereas server side code is not available to a client and is executed on a web server which generates the appropriate XHTML which is then sent to the client. The nature of client side coding allows you to alter the HTML on a local client and refresh the pages with updated content (locally), web designers must bear in mind the importance and relevance to security with their server side scripts. If a server side script accepts content from a locally modified client side script, the web development of that page is poorly sanitized with relation to security.<br><br>
<h3>Server side coding</h3>
<strong>
ASP (Microsoft proprietary),ActiveVFP (open source),CSP, Server-Side ANSI C,
,ColdFusion (Adobe proprietary, formerly Macromedia, formerly Allaire),CGI,Erlang,with Linux, Yaws, Mnesia, Erlang (LYME) solution stack,Groovy (programming language) Grails (framework),Java, e.g. Java EE or WebObjects,Lotus Domino,Node.js,Perl, e.g. Catalyst, Dancer or Mojolicious (all open source),
PHP (open source),
Python, e.g. Django (web framework) (open source),
Real Studio Web Edition,
Ruby, e.g. Ruby on Rails (open source),
Smalltalk e.g. Seaside, AIDA/Web,
SSJS Server-Side JavaScript, e.g. Aptana Jaxer, Mozilla Rhino,
WebDNA (WSC proprietary),
Websphere (IBM proprietary),
.NET and .NET MVC Frameworks (Microsoft proprietary).
</strong>
<p id="in">
The World Wide Web has become a major delivery platform for web development a variety of complex and sophisticated enterprise applications in several domains. In addition to their inherent multifaceted functionality, these web applications exhibit complex behavior and place some unique demands on their usability, performance, security and ability to grow and evolve. However, a vast majority of these applications continue to be developed in an ad-hoc way, contributing to problems of usability, maintainability, quality and reliability.(1)(2) While web development can benefit from established practices from other related disciplines, it has certain distinguishing characteristics that demand special considerations. In recent years of web development there have been some developments towards addressing these problems and requirements. As an emerging discipline, web engineering actively promotes systematic, disciplined and quantifiable approaches towards successful development of high-quality, ubiquitously usable web-based systems and applications.(3)(4) In particular, web engineering focuses on the methodologies, techniques and tools that are the foundation of web application development and which support their design, development, evolution, and evaluation. Web application development has certain characteristics that make it different from traditional software, information system, or computer application development.
Web engineering is multidisciplinary and encompasses contributions from diverse areas: systems analysis and design, software engineering, hypermedia/hypertext engineering, requirements engineering, human-computer interaction, user interface, information engineering, information indexing and retrieval, testing, modelling and simulation, project management, and graphic design and presentation. Web engineering is neither a clone, nor a subset of software engineering, although both involve programming and software development. While web engineering uses software engineering principles, web development encompasses new approaches, methodologies, tools, techniques, and guidelines to meet the unique requirements for web-based applications.</p><br>

</div>
<footer class="foot">
    Follow Us On...
    <div class="img">
<img src="images/f.png" class="facebook" alt="">
<img src="images/t.png" class="twitter" alt="">
<img src="images/g.png" class="google" alt="">
</div>
<form id="alert" method="post">
    Subscribe For Latest Alert's...<br>
<input type="text" name="subscribe" id="subscribe"><br>
<input type="submit" value="Subscribe Us" id="subscribeus"><br>
</form>
<div id="fnav">
<a href="index.php">Home</a>|
<a href="tutorial.html">Tutorial's</a>|
<a href="information.html" class="select">Information</a>|
<a href="about.html">About Us</a>|
<a href="contact.html">Contact Us</a>
</div>
</footer>

</body>
</html>

but that can't work

Are you saying that none of your pages are displaying $_SESSION['username'] ?

yes the index page only but when i put it in other pages it will not display the username who are loged in..
All i want that when a userlogin then then the username will display on all the pages in my website like (welcom username), and when it will logout then the username will not display, that's all.

in your login.php , add 2 line before if function, do it show 1 or not

 $row=mysql_num_rows($result);
 echo "total rows ->" .$row "<-";
 die('stop');
  if($row==1)

yes it show 1

in other pages where it is not working u say,

After session_start();

add following lines

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

See what is output

nothing show..

Is your code running from localhost or do you have a copy on an externally hosted platform?

iam using xammp, mean my code is runing on localhost.

Will your code always be running localhost or will you copy to a hosted server? if yes, you should try a couple of small test files and see if the problem is only on your local dev environment.

sory bro my issue is still not solved!

its loooks like configuration of session in php.ini

session cookies are need to set, session directory must be writable by script and other issues.

you read that link?

what is ./header.php or ./footer.php?
Is it possible that you do something to the session there ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.