I do not know why it isn't working at all
Let say we've 2 files [a.php and b.php]
Method 1
a.php
<?php
session_start();
$_SESSION['msg'] = "Hello world";
header("Location: b.php ");
exit;
?>
b.php
<?php
session_start();
echo $_SESSION['msg'];
unset($_SESSION['msg']);
?>
Results : Not working
Method 2
a.php
<?php
$msg="Hello world";
session_register("msg");
header("Location: b.php ");
exit;
?>
b.php
<?php
echo $msg;
session_unregister('msg')
?>
Results : Not working
so what ! i even downgrade my php to PHP Version 4.4.9 and set register_globals = On
and i know this is not secure but i need it to be working badly :(
so any idea
This:
header("Location: b.php ");
should be:
header("Location: b.php");
That extra space is being sent as part of the header; you don't want that.
Related
I'm stuck trying to pass variables between 2 PHP files a.php and b.php using require(). I learned that PHP variables have a single scope, which means that if a.php includes b.php, it can read its variables. However, I would like b.php to be able to read a variable from a.php with my current setup. For example, I can print out $msg if it is set in b.php but not a.php. Is there a simple way to do this?
a.php
<?php
$msg = "Welcome NEW USER ! We can't wait for you to check out our books!";
require("connect.php");
require("b.php");
$table = "SELECT * FROM `login`";
if ($query_run=mysqli_query($cnn,$table)){
$username = $_POST['username'];
$pw = $_POST['password'];
$sql = "INSERT INTO login (username,password) VALUES ('{$username}','{$pw}')";
if($cnn->query($sql)===TRUE){echo "Query ran successfully";}
else{echo "Error: ".$sql."<br>".$cnn->error;}
}
else{die("table connection failed");}
$cnn.close();
?>
b.php
<?php
echo "<h1>".$msg."</h1>
<form action='' method='post'>
Username: <input type='text' name='username'><br>
Password: <input type='text' name='password'><br>
<input type='submit' name='submit'>
</form>
"
?>
Using $_SESSION will allow you to pass data between different files.
in a.php
$_SESSION['myName'] = "John";
in b.php
echo $_SESSION['myName']
I'm not sure what your trouble is but your example should work. This most definitely works:
a.php
<?php
$from_a = 'hey';
require "b.php";
print $from_b;
b.php
print $from_a;
$from_b = 'okay';
The above is perfectly valid PHP and will work as intended with no errors. A script which is required by another script has full access to its variables and vice-versa.
If you make require an infinite cycling occurs,try require_once in each file.
But the best solution is to create a third file be it c.php and include both b.php and a.php in it
No need to use of session.
if PHP file is included in other PHP file then variables also accessible in included PHP file.
Example
<?php
$a = 1;
include 'filename.php';
?>
Then $a is accessible in filename.php.
use echo in included file and test..
Let me know if you feel any issue.
"which means that if a.php includes b.php, it can read its variables."
I think the concept is slightly different. When you call the language construct require, it will read and include the specified file as if it were written in the calling file itself.
So this:
<?php
// File a.php
$somevar = 6;
require "b.php";
?>
<?php
// File b.php
echo $somevar;
?>
Is effectively the same as
<?php
// File a.php
$somevar = 6;
// File b.php
echo $somevar;
?>
I am trying run a script called random_post_generator.php which should execute every time a user is logged in.
I am using this approach as an alternative to cron.
Here is how my session is currently created:
<?php
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
}
?>
But how do I say - "if session is active, then run this script"?
<?php
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
include 'random_post_generator.php';
}
?>
or you can use require 'random_post_generator.php'
If I understood correctly, you are trying to find out how to include a script of php (that is located in an outside .php file) inside your current file while using your previous code that checks if a user is logged in:
<?php
$root_directory_path = $_SERVER['DOCUMENT_ROOT'];
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
$pathName = $root_directory_path."myScript.php";//I am assuming here
//the script is located inside the root directory, and not in a sub
//directory
require($pathName);
}
?>
just remember that whatever php code is inside myScript.php has to have the <?php ?> tag surrounding it. Your code does not reuse the <?php ?> tag of the "calling" file.
Let me know if that worked for you
I have a simple authentication: you login in the login.php page and you are redirected to the home.php page.
This is the code of login.php:
if(pg_num_rows($rs) == 0){ //I search in db for a row with username and password
$errMess = "error";
pg_close($conn);
}else{
$row = pg_fetch_row($rs);
session_start();
$_SESSION['username']=$_POST["nick"];
$_SESSION['admin'] = $row[0];
pg_close($conn);
header("Location: /home.php");
}
now in the home I have the header done in this way:
<?php require_once("scripts/functions.php");
require_once("scripts/config.php");
session_start();
?>
<div id="siteHeader" class="headersLeft"><?php echo WELCOME;?></div>
<div id="userContainer" class="headersRight">
Logged as: <?php echo getDisplayName(); ?>
<?php if(isset($_SESSION['username'])) {?>
<button class="button" onclick="location.href='/logout.php';">logout</button>
<?php }else{ ?>
<button class="button" onclick="location.href='/login.php';">login</button>
<?php }
?>
</div>
it doesn't work: even if data is correct it still gives me "guest", the session variable is lost in the header passage..how come?
Solved: i was under windows and the default path to the temp folder, where php actually saves session files, was wrong: was "/tmp" and was not recognized.
I set it to "C:\php\tmp" and it worked: session file was not saved at all!
Write session_start(); on top of everything (right after
<?php
session_start();
require_once("scripts/functions.php");
require_once("scripts/config.php");
?>
or if still doesn't work then write your code like this:
<?php
ob_start();
session_start();
require_once("scripts/functions.php");
require_once("scripts/config.php");
?>
Also don't forget to put these two lines at the top of your login.php page.
Hope it helps :)
I'm guessing there's some more code after the if statement that continues to manipulate $_SESSION. That's where $_SESSION['username'] is assigned the 'guest' value.
Remember, header("Location: /home.php"); only sets a response header. It doesn't redirect immediately, stopping script execution.
Place a exit; command right after header() to prevent execution from reaching the rest of the code:
header("Location: /home.php");
exit;
this works for me:
session_save_path ( "" ) ;
session_start();
I created two files
1.php
2.php
which are in the same folder(i am using xampp).
In 1.php used session_start() and also used $_session['name']=abc. Then i opened 2.php to check whether session was created or not
2.php:
<?php
if(isset($_session['name'])){
echo $_session['name'];
}
else{
echo "no session found!!";
}
?>
and it keeps on saying "no session found!!"
Plz help ...
I searched a few sites n they say that by default d session is for whole folder containing
d script and session_set_cookie_params($lifetime,'/') (where $lifetime=60*60) is also nt helping.
On d other hand if at d end of1.php i use require("2.php") then abc is displayed.
What you have done is right in 1.php,
however, 2.php must start the session before using it.
2.php
<?php
session_start();
if(isset($_SESSION['name'])) {
echo $_SESSION['name'];
}
else{
echo "no session found!!";
}
?>
You're missing session_start() at the top of your 2.php file which is needed to access $_SESSION variables.
<?php
session_start(); // missing
if(isset($_SESSION['name']))
{
echo $_SESSION['name'];
}
else
{
echo "no session found!!";
}
?>
You need to call session_start(); again at the top of every page where you want to access $_SESSION variables, not only on the page where you want to initiate the session.
<?php
session_start();
if(isset($_SESSION['name'])){
echo $_SESSION['name'];
}else{
echo "no session found!!";
}
?>
I have the following pages:
page1.php
<?php
if (isset($_GET['link'])) {
session_start();
$_session['myvariable'] = 'Hello World';
header('Location: http://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/page2.php');
exit;
}
?>
Click Here
page2.php
<?php
print 'Here is page two, and my session variable: ';
session_start();
print $_session['myvariable']; //This line could not output.
exit;
?>
When I try output $_session['myvariable'] I did not get the result hello world message.
I could not find out the solution to fix it?
$_SESSION not $_session. Uppercase.
error_reporting(E_ALL); at the top of the script always helps in such case
session_start() has to be called before you send any output as it relies upon cookies to store the ID.
Also $_SESSION is uppercase
<?php
session_start();
echo $_SESSION['myvariable'];
echo 'Here is page two, and my session variable: ';
exit;
?>
HTTP headers must be the very first output, so session_start() must be at the top of your code.
Other notes:
* $_SESSION should be uppercase.
* Echo > print