Passing variables between PHP scripts - php

I have problem and can not understand why this not working. The language not transferred and I see empty area.
In my PHP:
<?php
$_SESSION['Language'] = $_GET['Spanish'];
header("Location:../index.php");
exit;
?>
In index.php:
<?php
$Language = $_SESSION['Language'];
include 'header.php';
echo '<div id="content">
<img src="Images/SplashScreen.jpg" id="bgImage" alt="City" width="1280" height="720" />
<div class="splashOuterRingsOverlay" id="contain">
<img src="Images/Loading-outer-circle.png" id="image1" alt="inner ring" width="282" height="282">
</div>
<div class="splashInnerRingsOverlay" id="contain">
<img src="Images/Loading-inner-circle.png" id="image2" alt="outer ring" width="282" height="282">
</div>
<div class="splashButtonOverlay">
<!--<button type="button" id="splashButton" onclick="loadData(20)"></button>-->
<button type="button" id="splashButton" onclick="loadLangCustom(\'Spanish\')"></button>
</div>
<div class="splashTitleOverlay">
<p id="splashTitle">'.$Language.'</p>
<!--<p id="splashTitle">Please press BERMAD icon</p>-->
</div>
</div>';
include 'footer.php';
?>

If you set the session variable you have to add the session_start at the top of the php page.
session_start()
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
Change your code like this
<?php
session_start();
if (isset( $_GET['Spanish'])) {
$_SESSION['Language'] = $_GET['Spanish'];
header("Location:../index.php");
exit;
}
?>

Add session_start() in the beginning of your code
--
<?php
session_start();
$_SESSION['Language'] = $_GET['Spanish'];
<?php
session_start();
$Language = $_SESSION['Language'];

You are missing session_start() in index.php and my.php

Related

How to pass variable between two web pages with PHP without using session

I need to know How to pass variable between two web pages with PHP without using session , have a DIV link in which inside there is id i want to pass to another page when a link is clicked but i fail to do it. please help me:
firstpage.php ( where there is a link) :
......
<?php
$identity = $_POST['book_id'];
while($ResultsRow=mysql_fetch_array($res)) {
?>
<div class="col-md-2 offset-md-2">
<div class='thumbnail'>
<a href="shopping.php?<?php echo $identity?>" target="blank">
<input type="text" name="book_id" value="<?php echo
$ResultsRow['id'] ?>">
</div>
</div>
</a> <!-- end of link -->
..........
Secondpage.php (where i want to retrieve book id):
....
<p> <?php $myvarC = $_POST['identity'];
echo $myvarC;
?> </p>
....
I think you should try something like below:
firstpage.php
<a href="shopping.php?identity=<?php echo $identity?>" target="blank">
secondpage.php
<p> <?php $myvarC = $_GET['identity'];
echo $myvarC;
?> </p>
You can include it in the urlquery like
shopping.php?id=1234
and then read it on the other side.
You can do this by $_GET;
First Page
<a href="shopping.php?id=<?php echo $identity?>" target="blank">
Second Page
<?php $myvarC = $_GET['id'];
echo $myvarC;
?>
Create a correct url with parameters
<a href="shopping.php?id=<?php echo $identity?>" target="_blank">
And get that value using
<?php
$myVarC = $_GET['id'];
echo $myVarC
?>
First Page
<a href="shopping.php?identity=<?php echo $identity?>" target="blank">
Second Page
<p> <?php $myvarC = $_REQUEST['identity'];
echo $myvarC;
?> </p>

php - can't retain session values when navigating to another page

I'm developing a php application and i have a problem retaining session values. I have two files, one is a sidebar (sidebar.php) and a home page (home.php). I have included the sidebar on the home page.
There are login controls on the sidebar and i can successfully login. I know it has successfully logged in because it shows me a message Welcome 'username'. But when i go to the home page, the welcome 'username' part is not shown as the session values are destroyed. instead the login form is shown. Why is that?
This is the home page (sidebar.php)
<?php require_once('connections.php'); ?>
<?php
// if the login button is clicked
if (isset($_POST['btnLogin']))
{
$myusername=$_POST['textusername'];
$mypassword=$_POST['textpassword'];
$result=mysql_query("SELECT * FROM users_table WHERE username='$myusername' and password='$mypassword'");
$count=mysql_num_rows($result);
if($count>=1)
{
$_SESSION['username'] = $row["username"] ;
$_SESSION['userid']= $row["ID"];
}
else
{
//Any code here
}
}
?>
<div class="col-md-12 right-aside">
<?php
if (isset($_SESSION['userid']))
{
echo Welcome : " . $_SESSION['username'];
echo " <a href='logout.php'> | Logout</a>";
}
else //if session is not set
{
echo 'Some html for login form';
}
?>
</div>
The following is the home page (home.php)
<?php include("head.php"); ?>
<body>
<div class="container container-body">
<div class="row">
<div class="col-md-9 main-content">
<div class="row">
<p>Some Text Here</p>
</div><!-- /.row -->
</div><!-- /.main-content -->
<div class="col-md-3">
<div class="row">
<?php include 'sidebar.php';?>
</div>
</div>
Are you starting a session in home.php?
You must call session_start() at the first line of every php script in which you want to access session variables.
Try adding this in top of home.
<?php
session_start();
//Then do your work
Then even if session_start is called again in head.php or sidebar.php , it will be ignored, as the session was already started.

Creating a simple "Logged In As" line on my page

<?php
session_start();
if(isset($_SESSION['login']))
{
include_once('includes/header.php'); ?>
<!DOCTYPE html>
<html>
<body>
<div id="mainframe">
<img src="img/header.png">
<div id="menu">
<?php include_once('includes/navbar.php'); ?>
</div>
<div id="content">
<h3>Shopping Cart</h3>
</div>
</div>
<?php include_once('includes/footer.php'); ?>
</body>
</html>
<?php }
else
{
header('location: login.php');
}
?>
Here is my small PhP code I've got at the moment, my login session is $_SESSION['login'].
And I'd like to display : Logged in As on my page when they are logged in, I've tried several things but it didn't work out.
Does anyone know a simple method / solution for this?
Put this somewhere in your if statement.
It will show Logged in as User at right top corner of page
<div style="position:absolute; right:0px; top:0px;">
<?php echo "Logged In as". $_SESSION['login']; ?>
</div>
U need to pass username using SESSION variable for the same
write a simple sql query to get the username from any variable you are taking from user to make sure that the particular user is the correct user.i am taking password.
$query = "SELECT name FROM users WHERE password='$password'";
$username = mysql_result(mysql_query($query),0);
$_SESSION['username'] = $username;
than proceed as you are doing
<?php
session_start();
if(isset($_SESSION['login']) && isset($_SESSION['username']))
{
echo "logged in as".$_SESSION['username'];
}

PHP use a variable from a different file

I have a login page which records the username that the user enters and adds it to a variable of $uname. However when the page after the login page loads, I cannot echo the $uname. For example, when i type
Welcome <?php echo $uname; ?>
it does not insert the username.
below is a copy of my login-validation code. but I am not sure if the $_SESSION variable is working correctly, or how to reference it in my profile.php file.
<?php
session_start();
$_SESSION['uname'] = $uname;
// Grab User submitted information
$uname = $_POST["uname"];
$pass = $_POST["pass"];
// Connect to the database
$con = mysql_connect("mysql.*********.co.uk","******","************");
// Make sure we connected succesfully
if(! $con)
{
die('Connection Failed'.mysql_error());
}
// Select the database to use
mysql_select_db("onedirectionaffection_members",$con);
$result = mysql_query("SELECT uname, pass FROM users WHERE uname = $uname");
$row = mysql_fetch_array($result);
if($row["uname"]==$uname && $row["pass"]==$pass)
header("Location: ../../profile/profile.php");
else
echo"Sorry, your credentials are not valid, Please try again.";
?>
If anyone could help I would be hugely thankful. Also, I am an absolute beginner at all of this so if you need anymore details I'll try my best to answer.
profile.php
<?php
session_start();
echo $_SESSION['uname'];
?>
<html>
<head>
<title>1D Affection</title>
<link rel="stylesheet" Type="text/css" href="../css/stylesheet.css" />
<link rel="stylesheet" Type="text/css" href="../css/font.css" />
<link rel="stylesheet" Type="text/css" href="../css/profile.css" />
</head>
<body bgcolor="white">
<div id="wrapperhead">
<div id="headcont">
<div class="logo">
<img src="../images/1DA logo ripped.png" height="150px">
</div>
<div class="subheading">
<img src="../images/1d subheading.png" height="150px">
</div>
</div>
</div> <!--END OF HEADER-->
<div id="nav">
<div class="navigation">
<ul>
<li><a class="nav" href="../index.html">Home</a></li>
<li><a class="nav" href="#">News</a></li>
<li><a class="nav" href="#">Fan-fiction</a></li>
<li><a class="nav" href="#">Gallery</a></li>
<li><a class="nav" href="#">Testimonials</a></li>
<li><a class="nav" href="http://www.onedirectionstore.com/" target="_blank">Store</a></li>
</ul>
</div> <!-- END OF MENU-->
<!-- END OF NAVIGATION-->
</div>
<div id="wrappercontent">
<div class="content">
<div class="maincont">
<div class="profcust">
<div class="profpic">
</div>
<div class="profinfo">
</div>
</div>
<div class="username">
Welcome <?php session_start(); echo $uname; ?>
</div>
<div class="story">
</div>
</div>
<div class="sidenav">
Coming Soon
</div>
</div><!--end of content-->
</div>
</body>
</html>
Seems like you haven't added session_start(); on top of your profile.php page.
Try like this
//profile.php
<?php
session_start();
echo $_SESSION['uname'];
This is probably a good part of the issue.
$_SESSION['uname'] = $uname;
$uname = $_POST["uname"];
Your setting your session's uname to blank on every load of that page. Put $_SESSION['uname'] = $uname; at the end of the code when it's validated.
1) You need to add a value to $uname first, then assign its value to $_SESSION element, so it's better be like this:
$uname = $_POST['uname'];
$_SESSION['uname'] = $uname;
or even like this:
$_SESSION['uname'] = $_POST['uname'];
2) As already mentioned, At profile.php you should also have session_start();
3) Make a clean exit like this:
header("Location: ../../profile/profile.php");
exit();
My bet is that it should be working fine after.
Some how, this is now working. From what I can figure out, the solution was to call in the $_SESSION variable, and then wrap that inside another variable. so
<?php
session_start();
$uname = $_SESSION['uname'];
?>
Thanks for all the help :D
session_start(); needs to be inside all pages using sessions.
I tested the following:
<?php
session_start(); // page_2.php
echo "Welcome " . $_SESSION['uname'];
?>
In conjunction with my test page: page_a.php
<?php
session_start();
$uname = "FRED";
$_SESSION['uname'] = $uname;
?>
CLICK
Echo'ed Welcome FRED on page 2.
I also noticed you have another instance of session_start(); in your page profile.php, remove it because you will be starting a new session while overwriting your first.
<div class="username">
Welcome <?php session_start(); echo $uname; ?>
</div>
Therefore you should be using:
$uname = $_SESSION['uname'];
in conjunction with:
<div class="username">
<?php echo "Welcome " . $_SESSION['uname']; ?>
</div>
As berkes stated in this comment you have a security issue:
$uname = $_POST["uname"];
$pass = $_POST["pass"];
Change it to:
$uname = mysql_real_escape_string($_POST['uname']);
$pass = mysql_real_escape_string($_POST['pass']);
MySQL_ functions are deprecated, therefore using MySQLi_ with prepared statements is highly suggested or PDO.
Do read the following articles:
How can I prevent SQL injection in PHP?
On owasp.org

Undefined index in php (_SESSION['id'] and ['user'])

I'm new to php and I'm following a tutorial to make a login panel. It works fine on the demo website but when I download the code and run on my machine, 5 notices popped up. They all look like: "Notice: Undefined index: submit in C:\xampp\htdocs\myfolder\demo.php on line 24".
From other programming experiences I think that these means I didn't define the variables before using them. However, these variables seem to be existing in the system (from what I understand reading other questions >-<).
I attached my code below and marked the undefined index on the right. Can anyone help me explain what's wrong with the code and how can I solve it? Thanks a lot!
<?php
session_name('Login');
session_start();
if($_POST['submit']=='Login') //undefined submit
{
$err = array();
if(!$_POST['username'] || !$_POST['password'])
$err[] = 'All the fields must be filled in!';
if(!count($err))
{
$_POST['username'] = mysql_real_escape_string($_POST['username']);
$_POST['password'] = mysql_real_escape_string($_POST['password']);
// Escaping all input data
$row = mysql_fetch_assoc(mysql_query("SELECT id,user FROM writers WHERE user='{$_POST['username']}' AND pass='".md5($_POST['password'])."'"));
if($row['user'])
{
$_SESSION['user']=$row['user'];
$_SESSION['id'] = $row['id'];
// Store some data in the session
}
else $err[]='Wrong username and/or password!';
}
if($err)
$_SESSION['msg']['login-err'] = implode('<br />',$err);
// Save the error messages in the session
header("Location: demo.php");
exit;
}
$script = '';
if($_SESSION['msg'])
{
// The script below shows the sliding panel on page load
...
}
?>
<head>
......
</head>
<body>
<!-- Panel -->
<div id="toppanel">
<div id="panel">
<div class="content clearfix">
<?php
if(!$_SESSION['id']): //undefined id
?>
<div class="left">
<form class="clearfix" action="" method="post">
<h1>Writer Login</h1>
<?php
if($_SESSION['msg']['login-err']) //undefined login-err
{
echo '<div class="err">'.$_SESSION['msg']['login-err'].'</div>';
unset($_SESSION['msg']['login-err']);
}
?>
//Login form
</form>
</div>
<?php
endif;
?>
</div>
</div> <!-- /login -->
<!-- The tab on top -->
<div class="tab">
<ul class="login">
<li class="left"> </li>
<li>Hello <?php echo $_SESSION['user'] ? $_SESSION['user'] : 'Guest';?>!</li> //undefined user
<li class="sep">|</li>
<li id="toggle">
<a id="open" class="open" href="#"><?php echo $_SESSION['id']?'Open Panel':'Log In';?></a> //undefined id
<a id="close" style="display: none;" class="close" href="#">Close Panel</a>
</li>
<li class="right"> </li>
</ul>
</div> <!-- / top -->
</div> <!--panel -->
Sorry for the long code! I really not sure which ones will be relevant to the problem and do not dare to delete more. Thank you for your patience!
You need to check if that variable exists before you use it. You would use isset() for that:
if(isset($_POST['submit']) && $_POST['submit']=='Login')
If you're just checking to see if the form was submitted you could just check to see if the page was request via POST instead:
if('POST' === $_SERVER['REQUEST_METHOD'])
You can also put # in front of your array references that can be null, for example:
if(#$_POST['submit']=='Login')
This will supress the warning

Categories