Getting value from session PHP - php

I am not sure why the variable username is not being returned in the session. When the user logs in, I start the session:
$username = trim($_POST['username']);
if(!isset($_SESSION)){ session_start(); }
$_SESSION[$this->GetLoginSessionVar()] = $username;
On the user's welcome page, when I run the echo command, I see the proper variable being returned. But I'm not sure why the return statement isn't working. I have the following in my PHP file:
function UserName()
{
return isset($_SESSION['name_of_user']) ? $_SESSION['name_of_user'] : "Unknown User" ;
//echo $_SESSION['name_of_user'];
}
In my html, I have:
Welcome back <?PHP $fgmembersite->UserName(); ?>!
I also checked the session ID, and it's also being generated properly.
Can you please help me understand what I'm doing wrong?

Is fgmembersite an object and have it the function called UserName ?
If yes, you simply miss an echo
<?PHP echo $fgmembersite->UserName(); ?>

You must add echo or print so should look like this;
<?PHP echo $fgmembersite->UserName(); ?>

You need to print out your variable. Use
Echo or print

Possibly you should add output:
<?php print $fgmembersite->UserName(); ?>

If you are using the script I think you are using, you need to look through fg_membersite.php at the line that says:
function CheckLoginInDB($username,$password)
whithin that line you should have a MySQL statement:
$qry = "SELECT etc...
When I tried to add UserAvatar I was able to do that by adding it to that MySQL string.
On a side note, I too am having trouble with adding UserName, and for the life of me I can't figure out why it would work any different than my previous workaround, yet somehow it is, but I am still convinced something in that file will do the trick eventually.
Edited:
Ok i got it, just do this:
echo $fgmembersite->UserName($username);
The username will pop right out. I have no idea why, i don't know enough php to explain it, but i can only assume this will get you going.

Related

How do i put a $ before a database tag

I try to make a website with an account system, but now i am stuck.
I already tries to do it with this code <?php echo "<p>$</p>"$userRow['username']; ?>
what i want to have is that there will be something like this $USERNAME and that has to transform into the thing that i put into my config file.
$daan0605 = ('CoOwner'); $mohagames205 = ('HeadCreator'); Sorry that i dont know all the terms yet,
but i hope you guys can help me :).
Variable variables is what you're looking for. In that case you would simply do,
<?php echo $$userRow['username']; ?>
So if $userRow['username'] outputs daan0605 and $daan0605 = 'CoOwner';, then the above statement would output CoOwner.

PHP Session Variable get lost and session id is changing on every request

i found many answers about that problem but nothing solved my problem - so i want to show you my code and hope that someone can find the mistake..
I have a standard HTML formular that gives some data with POST to the next .php file where i get it and save it into session-variables. I use the session variables about 2 reasons:
if someone reloads the page, it should show the same information as before.
I need the variables in upcoming php files.
Here is the code:
session_start();
// Handle Variables on post and reloaded-page
if(isset($_POST["locId"]) && isset($_POST["dateId"]) )
{
$locId = htmlspecialchars($_POST["locId"]);
$dateId = htmlspecialchars($_POST["dateId"]);
$_SESSION["locId"] = $locId;
$_SESSION["dateId"] = $dateId;
echo "Session variables are set: locId = " . $_SESSION["locId"] . " dateId = " . $_SESSION["dateId"];
} elseif(isset($_SESSION["locId"]) && isset($_SESSION["dateId"])) {
echo "get it from session";
$locId = $_SESSION["locId"];
$dateId = $_SESSIOn["dateId"];
} else {
$load_error = 1;
$status = "alert alert-danger";
$message = "shit, no variables here";
}
The frist call works fine - session variables are set and the echo gives the right values. After reloading the page i get the echo "get it from session" but my variables have no values.
i also checked my session_id() on first call and reload.. they are NOT the same.
I testet a simple test.php file where i start a session with a variable and ask for the variable in the next file. It works fine :-/
Its just a problem with my code above. I think my webserver is handling right. But what reasons are there for chaging a session id and losing session-variable values?
Damn! To write correct is everything ...
I found my mistake.
Look at the code in my question. The second session-variable is $_SESSIOn["dateId"].. the n is lowercase! If i write it correctly and complete in UPPERCASE it is working.
Also the session_id is not chaging anymore and i can output the session_id() as much as is want.. but one mistake in $_SESSIOn changes everything. New session_id on every call, ... strange.
Learned something again :-) Thanks to everybody for the answers and your time! I hope i can help you in the future
Well, your mistake is quite easy to find. In fact, your code works perfectly. But look at this part:
echo "get it from session";
$locId = $_SESSION["locId"];
$dateId = $_SESSIOn["dateId"];
Well, you asign the session values to two variables, but in fact, you simply missed to output them anywhere. Thats why you get "get it from session" but then is displays nothing, you need to echo them.
Simply add an echo and it will display your vars perfectly :)
echo "get it from session";
$locId = $_SESSION["locId"];
$dateId = $_SESSIOn["dateId"];
echo $locId;
echo $dateId;
Try this:
session_id();
session_start();

Using $_SESSION variables on a function

I have a function on my page to login it works like this:
function entrarSistema($email,$senha){
if(isset($email) and (autentica($email,$senha)!=false)){
$mysqli = connect_db();
$result = mysqli_query($mysqli,"SELECT ID FROM px_user WHERE email = '$email'");
$id = mysqli_fetch_array($result);
$_SESSION['nome'] = autentica($email,$senha);
$_SESSION['email'] = $email;
$_SESSION['password'] = $senha;
$_SESSION['ID'] = $id[0];
$_SESSION['logado'] = true;
}
else{
if(check_double($email)==1){
setCodeAlerta(1);
echo $_SESSION['status'];
}
else {
setCodeAlerta(2);
}
}
}
This function works fine, and the Session variables are set when i call the function setCodeAlerta(), the Session variable i've declared wont work. Notice that this 2 functions are on the SAME file Here is the function:
function setCodeAlerta($numeroCodigo){
$_SESSION['status'] = $numeroCodigo;
}
My index.php has all the pages included, and i use url_rewrite to add the piece of codes that i need, and has this on its very top:
if( !isset($_SESSION) ){ session_start(); }
Oddly enough, if i call directly a file named test.php with this code:
<?php
setCodeAlerta(2);
?>
The variable is set fine, and everything works well.
Thanks in advance.
I'd add a comment, but my rep isn't high enough yet. Out of curiosity are the other session vars available at that point that you are setting? -- I ask because I'm running your code (stripped down) and it's returning OK. I'm just thinking there's something more to this than just that. Are you calling session_start() twice anywhere in an include or anything?
I got that. Everything was absolutely fine with my code, i just did a conditional, where my code would never really go to wrong username/password, hence, not accessing my function (wich is working fine). After removing the second part (after the and) part of this conditional, my code worked just fine.
if (isset($_POST['submitLogin']) and autentica($_POST['emailLogin'],$_POST['passwordLogin']){
entrarSistema($_POST['emailLogin'],$_POST['passwordLogin']);
}
I normally ask questions and find my answer minutes after that, i think thats a bad habit of mine. Even so, i spent the last 5 hours debugging it.
Thanks!

Display data if usertype is admin otherwise display error message

I currently have a list of users in my mysql database. One of the columns is "type". I am trying to display certain data if type is equal to admin. If type is equal to anything else, it should just echo an error message.
Unfortunately, I have tried multiple methods but it just does not seem to be working out for me. Can anyone help me get this to work properly?
This is what I have, but obviously I am doing something wrong....
<?php
$usertype = $_SESSION['type'];
if ($usertype == "admin" ){
?>
admin stuff only goes here
<?
}
else
{
echo "not priveleged usertype";
}
?>
EDIT:
The following code works when displaying via username, however, I need content displayed by usertype, not the username.
<?php
if($_SESSION['user']['username'] == "oneoftheadminusernames" )
{
?>
Each page has to start with
<?php
#session_start();
?>
otherwise, php does not "see" the sessions contents. So that's probably it.
The # prevents the php error: A session has already been started... by the way.
Now, every page that uses the session must have this directive at the top.
At least, in a quick example, that reproduces your error perfectly.
If you are saving each logged in users type field in $_SESSION['type'] variable than the code you are writing is correct. Or if you are storing type in another variable than you that variable to check.
i have an idea like add a field EnableFlag in the table. if enablee flag is set to 1 consider it as a admin else as a User;

Post variable not being caught in php sent from dynamically generated html

Im having a really simple issue but iv looked around and cant debug it for some reason, can someone point me in the right direction??
I have a php script which dynamically generates a link
<?php
$id = 1;
echo "<a href='http://www.example.com/page.php?id='$id'>click link</a>"
?>
On example.php I have...
$userId = $_POST['id'];
then I insert $userId query...
?>
For some reason the Post vairable is not being cause by the example.php script I can see it in the URL at the top of the page but they wont make sweet passionate php love. Any thoughts? I will mention I am doing this from within an IFRAME however I tried it simply and got the same result :(
I think you mean, on page.php you have...
If that is the case, you are sending the id parameter in a GET, not a POST. To access it in your other page you need to use:
$userId = $_GET['id'];
your variable is in $userId = $_GET['id'];.
another problem is a mess with ' symbols: should be
echo "<a href='http://www.example.com/page.php?id=$id'>click link</a>"
Sorry, but you ar sending data via GET NOT POST
access it via $_GET['id'];

Categories