I want to create session for the record tag_id here is my sql code please help me how i create session for this value..
<?php
$sql_tagid=mysql_query("SELECT * FROM tag_new WHERE EMAIL_ID='$sessionemail' AND CUST_ID='$id'") or die(mysql_error());
while($tagid=mysql_fetch_array($sql_tagid)){
echo $tagid['tag_id'];
}
?>
First you need to start session
session_start();
after that add variable to session
$_SESSION['tag'] = $tag['tag_id']
after that you can check by print session
print_r($_SESSION); you value in session or not
$_SESSION['tag'] = $tagid['tag_id']
Try this, if you have mutliple tag id then use it $_SESSION['tag_id'][], if you have only one $_SESSION['tag_id']
session_start();
while($tagid=mysql_fetch_array($sql_tagid)){
$_SESSION['tag_id'][] = $tagid['tag_id'];
}
var_dump($_SESSION['tag_id']);
<?php
session_start(); // To assign a session variable you have to start the session
$sql_tagid=mysql_query("SELECT * FROM tag_new WHERE EMAIL_ID='$sessionemail' AND CUST_ID='$id'") or die(mysql_error());
while($tagid=mysql_fetch_array($sql_tagid))
{
$_SESSION['tag_id'] = $tagid['tag_id']; // assign tag_id value to session variable
echo $tagid['tag_id'];
}
?>
For getting the value of session on another page say page1.php then you have to do :-
page1.php
<?php
session_start();
echo $_SESSION['tag_id'];
?>
Related
Am trying to set a session in foreach loop when users post a form so when they refresh the page the post values won't lost, but the session is not saving.
My Code
<?php session_start();
if(isset($_POST['EventLookUp'])){
/* city state zipcode country*/
$EventPostArgs = filter_input_array(INPUT_POST);
foreach($EventPostArgs as $pname => $pval){
$_SESSION[$pname] = htmlentities($pval);
}
echo $_SESSION['country'];
?>
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.
this is login.php and its header location is index.php and i want to store session id on index page but nothing has been shown
<?php
#extract($_POST);
include("config.php");
$sql="select password,name,us.id as uid from us left join tblblo on (tblblo.maker=us.id) where name='$websitename' and password=md5('$password')";
$result=$db->query($sql);
if($count=mysql_num_rows($result)>0)
{
$row=mysql_fetch_assoc($result);
$_SESSION['name']=$row['name'];
$_SESSION['id']=$row['uid'];
header("location:http://".$_SESSION['name'].".domain.com");
}
else
{
//$_SESSION['sess_msg']="Invalid Web Name or Password!Try Again.";
//header("location:https://domain.com/login.php");
//exit;
}
?>
i want to store session id on index page but nothing has been shown
1) It's always good to write session_start(); in the very beginning of your code before outputting anything else..
<?php
session_start();
#extract($_POST);
include("config.php");
2) You're mixing up the deprecated mysql API's with the object_oriented ones.
Change this:
if($count=mysql_num_rows($result)>0)
{
$row=mysql_fetch_assoc($result);
$_SESSION['name']=$row['name'];
$_SESSION['id']=$row['uid'];
To this:
$count = $result->num_rows;
if($count > 0)
{
$row = $result->fetch_assoc());
$_SESSION['name']=$row['name'];
$_SESSION['id']=$row['uid'];
3) this is login.php and its header location is index.php and i want to store session id on index page but nothing has been shown
I think what you meant was to use/echo the session values on index.php rather store?
on your index.php (after successfully storing the sessions on login.php):
<?php
echo $_SESSION['name'];
echo $_SESSION['id'];
?>
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.
the first example
<?php
session_start();
if(!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else ++$_SESSION['count'];
echo $_SESSION['count'] . "<br />";
?>
the second example
<?php
session_start();
if(!isset($_SESSION['initiated']))
{
session_regenerate_id();
$_SESSION['initiated'] = 1;
}
if(!isset($_SESSION['count'])) $_SESSION['count'] = 0;
else ++$_SESSION['count'];
echo $_SESSION['count'] . "<br />";
I can find the only different is if I use two different URLs(eg: http//localhost/test?PHPSESSID=123; http//localhost/test?PHPSESSID=456) the first script will count up again from zero, but the second will continue count
so what do two examples mean? and what does it want to tell me?
Sorry, my first answer was too hasty. Check This out. PHP.net says:
"session_regenerate_id() will replace the current session id with a new one, and keep the current session information."
session_regenerate_id() if it can, will submit a cookie to keep the session details.
Think of it as a extended session for the user. So that if the user closes their browser, the user session can be recalled.