We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,650 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP & Sessions: Please Help

Hi, I am trying to check if a certain username is in a session. This is what I have so far...

<?php

if ($_SESSION=='Djmann1013') {

// Do the work here.

} else {

// Don't do anything :P

}

?>

But, when I test this, it does nothing. But when I do this:

<?php
if ($_SESSION='Djmann1013') {

// Do the work here.

} else {

// Don't do anything :P

}
?>

It does what I want, but it overrides the $_SESSION variable. But all I want is to make it so that when a person logs on with the name "Djmann1013" it will do what I want it to, and what I want it to do when it's not the person, with out overriding the $_SESSION variable.

Big thanks if you help.

3
Contributors
4
Replies
18 Hours
Discussion Span
11 Months Ago
Last Updated
5
Views
Question
Answered
Djmann1013
Junior Poster
106 posts since May 2012
Reputation Points: 0
Solved Threads: 3
Skill Endorsements: 0

Hi,

You should use the $_SESSION like this :

session_start(); // initialisation, we must always use it with sessions

$_SESSION['data'] = value; // put data to session

$var = $_SESSION['data']; // get data from session

In your case :

<?php

    // attention here, we use 2 '=' ( == ) to compare
    // 1 '=' ( = ) affect value to the $_SESSION array

    if ($_SESSION['username'] == 'Djmann1013') {     
        // Do the work here.
    } else {
        // Don't do anything :P
    }

?>
akmozo
Light Poster
48 posts since Jul 2012
Reputation Points: 5
Solved Threads: 9
Skill Endorsements: 0

Ill try this. Thanks.

Djmann1013
Junior Poster
106 posts since May 2012
Reputation Points: 0
Solved Threads: 3
Skill Endorsements: 0

$_SESSION is a super-global array, so treat it as an array

brandonrunyon
Junior Poster
154 posts since Sep 2007
Reputation Points: 32
Solved Threads: 14
Skill Endorsements: 0

I got it fixed. Thanks to those who replied. :D

Djmann1013
Junior Poster
106 posts since May 2012
Reputation Points: 0
Solved Threads: 3
Skill Endorsements: 0
Question Answered as of 10 Months Ago by brandonrunyon and akmozo

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0819 seconds using 2.7MB