How to store a value in a session? - php

I am displaying the user names as links on a php page like this which on clicking navigates on particular user home page:
$msql=$db->prepare("SELECT * from users where id=? order by id desc");
$msql->bind_param("i",$user_id);
$msql->execute();
$msql = $msql->get_result();
$msql = $msql->num_rows;
while($usercount=$msql->fetch_assoc())
{
$Email = $usercount['email'];
$FirstName = $usercount['first_name'];
$LastName = $usercount['last_name'];
?>
<strong><?php echo $FirstName.' '.$LastName;?></strong>
<?php
}
?>
And for the navigation to the user home page on clicking the link, I am using like this :
<?php
if(isset($_GET['navigate']) && $_GET['navigate'] == "true"){
$_SESSION['email'] = $Email;
header('location: nextpage.php');
}
?>
So my page looks like this as link
user1
user2
user3
.
.
.
usern
My issue is whenever I click on any of the links it always stores the first email in the session variable.
So, if I output the SESSION in nextpage.php
echo $_SESSION['email'];
it always echoes the first link email.
My guess for this because of the while loop I am using it always picks up the first link data and stays with it, but my question is how do i get the data for others as well. I want to navigate to that user page for which the link is clicked and that can only be done if I get the correct email on clicking the link.

As you said id is not primary key, and i assume that your same id will contain different emails, you have to do below changes:-
<?php
$msql=$db->prepare("SELECT * from users where id=? order by id desc");
$msql->bind_param("i",$user_id);
$result = $msql->execute(); // assign to a variable
//$msql = $msql->get_result(); //you are over-writing to one variable which is not correct
//$msql = $msql->num_rows; //you are over-writing to one variable which is not correct
while($usercount=$result->fetch_assoc()){
$Email = $usercount['email'];
$FirstName = $usercount['first_name'];
$LastName = $usercount['last_name'];
?>
<strong><?php echo $FirstName.' '.$LastName;?></strong> <!-- send mail id to the url too otherwise how's you will get the email id to save it into SESSION-->
<?php } ?>
AND
<?php
if(isset($_GET['navigate']) && $_GET['navigate'] == "true"){
$_SESSION['email'][] = $_GET['email']; // assign each mail to SESSION ARRAY not SESSION variable
header('location: nextpage.php');
}
?>
AND
echo "<pre/>";print_r($_SESSION['email']); //to see all emails.
Note:- You have to write session_start(); on top of your php page just after <?php if you want to work with SESSION on that page.

Related

How do I change my page to display different information based on how the user access it?

I have a page called profiles.php that displays the users own information when logged in. Recently I made it so the user can click on the name of someone else and it will take them to profiles.php. I want it to display the user's they clicked on profile/information, but it only shows your own information.
The way I have the page now is, it uses a session varaible based on if your logged in and from there it puts your data out in its designated places if that makes sense.
//if theuser is logged in then it turns their database id into a variable for later use
if(isset($_SESSION['userUid'])){
$current = $_SESSION['Id'];
}
//This is the link a person clicks on thats supposed to take them to that user's page. $post is a varaible from a foreach. I tried to make it so once they click it takes them to profiles.php users id (href = "profiles.php/id"'.$post["idUser"].'")
<a href = "profiles.php/id"'.$post["idUser"].'" ><h1>'.$post["UserID"].'</h1></a>
I expect the output to be taking the the current user to the desired user's profile page, but the actual output is taking the current user to their own profile page.
From what I understand you would like to show specific users profile information.
// define your connection
require_once('connection.php');
$users = mysql_query($conn, "Select id, name from users);
$row = mysql_fetch_array($users)
?>
<ul>
<?php
while($row){
echo <? <a href='profile_display.php?id=<?php $row['id'] ?> > <li> <?php $row['name'] ?> </li></a> ;
}
</ul>
in profile display.php
<?php
require_once'connection.php' ;
$id = $_GET['id'];
if(isset($id)) {
$result = mysql_query($conn, 'select * from users where id=$id);
if(!empty($result)) {
// display all value here
} else {
echo "No user profile information was found!"
}
?>
This code is not tested by me. But I would say this is the strategy needed to be used here.
Pseudocode:
List all users
User click specific other user, grab their id
make a query on db for the user information
display if available else display error information
Hope this helps!

How to pass links with values to pahe2.php

This is a fragment of my code, that get all users from Active Directory (some fields). The first column (user_name) is a reference to the page2.php where I can see User profile more detail. The problem is page1.php passes only last values of the link from the cycle. How can I do that, any dynamic link of the user passes its information. Could you help me? Thanks. $info is an array of LDAP data.
Here is my code:
<?php for ($i=0; $i<$info["count"]; $i++) {
$res=$info[$i];
$m_name=$res["displayname"];
$m_title=$res["title"];
$m_dep=$res["department"];
$m_tel=$res["telephonenumber"];
$m_mail=$res["mail"];
?>
<?php echo '<div class="column"><h5>'.$m_name[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_title[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_mail[0].'</h5></div>'?>
<?php } ?>
Depending on exactly what you are trying to do, you can do it in two ways. Based on what you described, and your comment about Sessions, you can pass information from page to page using a session.
http://php.net/manual/en/session.examples.basic.php
session_start();
$_SESSION['displayname'] = $res["displayname"];
$_SESSION['title'] = $res["title"];
$_SESSION['department'] = $res["department"];
$_SESSION['telephonenumber'] = $res["telephonenumber"];
$_SESSION['mail'] = $res["mail"];
And then on Page2, read the values back or use them directly on the page:
$name = $_SESSION['displayname'];
or
echo 'Hello ' . $_SESSION['displayname'] . ' how are you?';
But if Page1 is a list of all users, how would you determine what information to store in the session for passing to Page2?
It would make more sense to me to use GET parameters to pass information from page to page. So, for example, if Page1 is a list of all users, and Page2 information about a single user, you could structure your loop like this:
<?php for ($i=0; $i<$info["count"]; $i++) {
$res=$info[$i];
$m_name=$res["displayname"];
$m_title=$res["title"];
$m_dep=$res["department"];
$m_tel=$res["telephonenumber"];
$m_mail=$res["mail"];
?>
<?php echo '<div class="column"><h5>'.$m_name[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_title[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_tel[0].'</h5></div>'?>
<?php echo '<div class="column"><h5>'.$m_mail[0].'</h5></div>'?>
<?php } ?>
The unique ID from LDAP is just that. Something unique to that user that you can use to search on Page2 and pull out all of the details you want to print. It should be present in your $res array already.
So Page2 would have something like this at the top:
if(!isset($_GET['id']){
//Give an error if the ID isn't in the URL
}
$id = $_GET['id'];
$result = loadFromLDAP($id);
//Do stuff with the resulting $result array
loadFromLDAP being a function you would write to get details about a user from LDAP. Hope this helps get you on the right track.

Session Variable in PHP Issue Occurs

In my page, I have:
1. Registration Page
2. Login Page
3. Successful Registration Page
4. Referral Form
In my registration Page, User can register thru this.
In my Log-in page, I have two types of user, Applicant and Employee
In my Successful Registration Page, there will be a button there going to Referral Form.
In my Referral Form Page, I have a modal there to update referral information provided by the user during the registration.
The following information are:
Referrer ID
Fullname
Current Position
ContactID
Email Address
MObile Number
Member Since
If you created an account on my page, either you are a Applicant or Employee, if you successfully register, my successful registration page will prompt to you and once you have click the button going to Referral Form The following information will be displayed to your referral information based on you supplied during the registration.
If you register as an Applicant, your Referrer ID is always set into 0 and you may edit it thru Referral Form Page
or if you register as an Employee, your Referrer ID is based on you provide during the registration._
Example:
Referrer ID (Allowed to edit if you register as an applicant)
Fullname Sherlock Holmes
Current Position (This has no value and may be edit once you created an account)
ContactID CON12344
Email Address SherlockHolmes#gmail.com
MObile Number +987676758857
Member Since 2014-05-06 04:41:21
Here's my problem that I encounter.
I created an account and Successful registration page prompt to me, and I click the button going to Referral Form Page to edit my information. I edit it and Log-it out and try to relog-in, My Information updated and now reflecting on my Information. It works well.
But
When I created an account and promt successful registration page and click the button going to referral form page, If I did not edited my information and tried to log it out and try to re-login, my information becomes having all null values. LIke this,
Referrer ID 0
Fullname
Current Position
ContactID
Email Address
MObile Number
Member Since
Which was incorrect because even I did not edit my information, my information should just becomes like this.
Referrer ID 0(You can edit it)
Fullname Sherlock Holmes
Current Position (You can edit it)
ContactID CON12345678
Email Address sherlockholmes#gmail.com (You can edit it)
MObile Number +93456789 (You can edit it)
Member Since 2014-05-06 04:41:21
Problem Occurs when I didn't edit my information for a new created account, but when I edit it before I log it out, it's okay.
here is my Successful registration PHp
<?php
include('../include/dbconnection.php');
include('../include/functions.php');
if(!isset($_SESSION))
{
session_start();
}
$empid = $_SESSION['SESS_EMP_ID'];
$conid = $_SESSION['SESS_CONID'];
$fName = $_SESSION['SESS_FIRSTNAME'];
$lName = $_SESSION['SESS_LASTNAME'];
$contactNo = $_SESSION['SESS_CONTACT_NO'];
$mobile = $_SESSION['SESS_MOBILE'];
$email = $_SESSION['SESS_EMAIL'];
$bday = $_SESSION['SESS_BDAY'];
if($conid == '')
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='index.php';
</SCRIPT>");
}
else
{
//Nothing
}
?>
Here is my code in Referral Form
/**** Start Session ****/
session_start();
//Check whether the session variable SESS_EMP_ID is present or not
if(!isset($_SESSION['SESS_EMP_ID']) || (trim($_SESSION['SESS_EMP_ID']) == '')) {
header("Location: LoginPage.php");
exit();
}
/**** End ****/
/**** Redirects automatically to index ****/
header("Refresh: 15 * 60; url=index.php");
/**** End ****/
/**** authentication ****/
//require_once('../function/auth_emp.php');
/**** End ****/
$empid = $_SESSION['SESS_EMP_ID'];
$bdate = $_SESSION['SESS_BDAY'];
/**** Database connection ****/
require_once('../include/config.php');
/**** End ****/
include'../GlobalConstants.php';
include_once ('../refer/updateInfo.php');
mysql_select_db($db_name, $con) or die("ERR_COULD_NOT_SEE_DB");
if($empid == 0)
{
$fname = $_SESSION['SESS_FIRSTNAME'];
$lname = $_SESSION['SESS_LASTNAME'];
$bdate = $_SESSION['SESS_BDAY'];
$pos = $_SESSION['SESS_POSITION'];
$empid = $_SESSION['SESS_EMP_ID'];
$qry= "SELECT vtiger_contactdetails.firstname,
vtiger_contactdetails.contact_no,
vtiger_contactscf.cf_703,
vtiger_contactscf.cf_715,
vtiger_contactscf.cf_717,
vtiger_contactdetails.email,
vtiger_contactdetails.lastname,
vtiger_contactdetails.mobile,
vtiger_contactdetails.contactid,
vtiger_crmentity.createdtime
FROM vtiger_contactdetails
INNER JOIN vtiger_contactscf
ON vtiger_contactdetails.contactid = vtiger_contactscf.contactid
INNER JOIN vtiger_crmentity
ON vtiger_contactdetails.contactid = vtiger_crmentity.crmid
INNER JOIN vtiger_contactsubdetails
ON vtiger_contactsubdetails.contactsubscriptionid= vtiger_contactdetails.contactid
WHERE vtiger_contactdetails.firstname = '".$fname."'
AND vtiger_contactdetails.lastname = '".$lname."'
AND vtiger_contactsubdetails.birthday = '".$bdate."'";
$result = mysql_query($qry);
} else
{
$qry= "SELECT vtiger_contactdetails.firstname,
vtiger_contactdetails.contact_no,
vtiger_contactscf.cf_703,
vtiger_contactscf.cf_715,
vtiger_contactscf.cf_717,
vtiger_contactdetails.email,
vtiger_contactdetails.lastname,
vtiger_contactdetails.mobile,
vtiger_contactdetails.contactid,
vtiger_crmentity.createdtime
FROM vtiger_contactdetails
INNER JOIN vtiger_contactscf
ON vtiger_contactdetails.contactid = vtiger_contactscf.contactid
INNER JOIN vtiger_crmentity
ON vtiger_contactdetails.contactid = vtiger_crmentity.crmid
WHERE vtiger_contactscf.cf_739 = '".$empid."'";
$result = mysql_query($qry);
}
if($result)
{
if(mysql_num_rows($result)> 0)
{
$row = mysql_fetch_assoc($result);
$contact_no = $row['contact_no'];
$fname = $row['firstname'];
$mname = $row['cf_703'];
$lname = $row['lastname'];
$mobile = $row['mobile'];
$pos = $row['cf_715'];
$program = $row['cf_717'];
$email = $row['email'];
$conid = $row['contactid'];
$memberdate = $row['createdtime'];
}
}
$erp = "ERP";
/**** Stores the firstname and lastname in the session ****/
$_SESSION['SESS_EMP_ID'] = $empid;
$_SESSION['SESS_CONID'] = $conid;
$_SESSION['SESS_FIRSTNAME'] = $fname;
$_SESSION['SESS_MIDDLENAME'] = $mname;
$_SESSION['SESS_LASTNAME'] = $lname;
$_SESSION['SESS_MOBILE'] = $mobile;
$_SESSION['SESS_EMAIL'] = $email;
$_SESSION['SESS_POSITION'] = $pos;
$_SESSION['SESS_GEN'] =$erp;
$_SESSION['login_time'] = time();
?>
Do I have problem passing the session variable when the user didn;t fill up the information after they created an account?
If user edit and fill up all information and try to relogout and relogin. It seems okay and works.
But after user created an account, and If he didn;t edit the information and log it out and try to relogin, it does not reflect the values. Can you help me with this

issue in session variables

I am new to php.
I want to use form data that are sent through <form method="POST" action="formdata.php"> to formdata.php be used in another file called main.php. For this I ucerated session variables in formdata.php
Hers's my code in formdata.php
<?php
session_start();
include_once("connect.php");
$n=$_POST['name'];
$p=$_POST['password'];
$sql=mysql_query**strong text**("SELECT * FROM member WHERE `userName`='$n' AND `password`='$p'");
if(mysql_num_rows($sql)==1){
$_SESSION['user']=mysql_fetch_array(mysql_query("SELECT * FROM member WHERE `userName`='$n' AND `password`='$p'"));
if($_SESSION['user']){
$user=$_SESSION['user'];
$_SESSION['userN']="$user(['userName'])";;
$_SESSION['level']="$user(['level'])";
//header("location:mainPage.php");
echo $user['level'];
echo $_SESSION['level'];
}
}
else{
echo "invalid user name or password" ;
}
?>
But when I echo $user['level'];
echo $_SESSION['level']; all I get printed is 3Array(['level']). Here echo $user['level'] gives the desired out put 3, but echo $_SESSION['level'] gives an array as Array(['level']). What can I do to make it print 3?
My next question is I want to use this level value in another php file (main.php). I lerant that session variables are global. So can I use $user['level'] or should I use $_SESSION['level']. In main.php I want to check the condition
if($user['level'] == 3) {
echo "level 3 user";
}
The issue is this line: $_SESSION['userN']="$user(['userName'])";;
You're setting $_SESSION['userN'] equal to whatever $user is in string form which is Array, and (['username']).
Why are you even setting userN? $_SESSION should already contain the user in question, and you can just retrieve it anywhere to get the contents.
This should be all you need:
<?php
session_start();
include_once("connect.php");
$n=mysql_escape_string($_POST['name']);
$p=mysql_escape_string($_POST['password']);
$sql = mysql_query("SELECT * FROM member WHERE `userName`='$n' AND `password`='$p'");
if(mysql_num_rows($sql)==1)
{
//You can reuse the original $sql here. No need to run another query.
$_SESSION['user']=mysql_fetch_array($sql);
//If mysql_num_rows($sql) returns 1, $_SESSION['user'] should always be equal to a user array (Which is true).
header("location:mainPage.php");
}
else{
echo "invalid user name or password" ;
}
?>
On the other page (mainPage.php), just use session_start(), and check to see what is inside $_SESSION. This script should give you a jump start:
<?php
session_start();
print_r($_SESSION);
//This will get the level you're looking for
$level = $_SESSION['user']['level'];
?>
It should contain the user/level and any other information you need.

Undefined index when trying to get information from a 2nd page

I use my session with sql Information but I get a really weird error.
I have 2 pages at the moment. Now I have this in my 1st page:
while ($thread = mysqli_fetch_assoc($sql_result)) {
echo"<div id='question'<h2>
{$thread['title']}</br>
</h2>
<p style='font-size:16; text-align:left;'>
{$thread['description']}
</p>
<center>
<a href=profile.php?thread_username={$thread['username']}> {$thread['username']}</a></br>
{$thread['date_made']} </center></div>";
$_SESSION['profileuser']=$thread['username'];
}
So when you click the link, $_SESSION['profileuser'] will get the thread username.
Here's what I have in my 2nd page:
$profileusername = $mysqli2->real_escape_string($_SESSION['profileuser']);
The error I get is:
Undefined index
My other sessions work perfectly but this probably just wont do.
Of course I have session_start(); on every page...
When I press this button(on the 2nd page) it gives me the error:
if ( isset( $_POST['submit'] ) ) {
$about = $_POST['about'];
$sql_result2 = $mysqli->query("update account_information SET about='".$about."' WHERE username = '".$_SESSION["username"]."'");
$edit=false;
}
EDIT: I fixed it.
Paste this code to your second page:
<?php
session_start();
$var=$_SESSION['profileuser'];
if(empty($var))
{
die("Session variable is empty");
}
?>
it will help you to know the root of problem
Change:
$profileusername = $mysqli2->real_escape_string($_SESSION['profileuser']);
To:
$profileusername = mysqli_real_escape_string($_SESSION['profileuser']);

Categories