Simple PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Think-Digitally
Forum Commoner
Posts: 25
Joined: Wed Feb 04, 2004 2:25 pm

Simple PHP

Post by Think-Digitally »

Hi, I am having a problem with the PHP below:

Code: Select all

session_start();

echo "<font size=1 face=Verdana color=#CCCCCC>Welcome ". $_SESSION['first_name'] ." ". $_SESSION['last_name'] .". You are now signed  in.</font><br /><br />";
What I need is for this script to be applied if the user is signed in. If they are not signed in I need it to say that they arent signed in. So I think it needs an 'else' php term. But I am a beginner to PHP.

Is there any chance that anyone can show me what I need to do?

Thanks
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

something like this:

Code: Select all

<?php


if(!empty($_SESSION['firstname'])){
  echo "<font size=1 face=Verdana color=#CCCCCC>Welcome ". $_SESSION['first_name'] ." ". $_SESSION['last_name'] .". You are now signed  in.</font><br /><br />"; 
}else
{
  echo "<font size=1 face=Verdana color=#CCCCCC>Please sign in.</font><br /><br />"; 
//or redirect to log in screen or whatever you want to do
}

?>
But I would do something in the log in script like set a $_SESSION[loggedin'] boolean to check rather than the firstname
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Not in one script

Post by Lord Sauron »

Edited because:
viewtopic.php?t=21171
Think-Digitally
Forum Commoner
Posts: 25
Joined: Wed Feb 04, 2004 2:25 pm

Post by Think-Digitally »

Hi magicrobotmonkey

This is a great script, but for some reason even if I am signed in it says 'Please sign in'.

Anyway that theres anyother way I can get it to work?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

at the top of your page put

Code: Select all

<?php
if(isset($_SESSION['firstname'])){
  echo $_SESSION['firstname'];
}else{
  echo "IT's not even set!";
}
?>
Think-Digitally
Forum Commoner
Posts: 25
Joined: Wed Feb 04, 2004 2:25 pm

Post by Think-Digitally »

Its just says 'IT isnt even set' in the top corner.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

So, what's your log in script look like - that is, what happens before you get to this page?
Think-Digitally
Forum Commoner
Posts: 25
Joined: Wed Feb 04, 2004 2:25 pm

Post by Think-Digitally »

Well when you login on the login page, it uses checkuser.php against the db.
The whole point of the this script I want is to go at the top so it would say Welcome name name, you are signed in, when you have signed in. And if you are logged out it would say please sign in, or register here
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

try this

Code: Select all

<?php

session_start();
$_SESSION['firstname'] = "foobar";

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

?>
this will print everything in the array, this way you can see if its even getting set.
Post Reply