如何从外部 javascript 中的 php 访问会话变量
How do I access session variable from php in external javascript
我知道之前有人问过这个问题,但是从头到尾都没有解释。我有一个 login.php
,我想从中得到 $_SESSION['user_id']
来自
<?php
include "connect.php";
session_start();
$email=$_POST['email'];
$password=$_POST['password'];
$select_data=mysql_query("select * from user where email='$email' and password='$password'");
if(isset($_POST['email']))
{
if($row=mysql_fetch_array($select_data))
{
$_SESSION['user_id']=$row['id']; //The variable want to access from ext.js
echo "success";
}
else
{
echo "fail";
}
exit();
}
?>
顺便说一句,这个认证工作正常
我的Main.php
<!DOCTYPE html>
<?php include 'connect.php';
session_start();
echo $_SESSION['user_id']; //works fine
//;
?>
<body>
//contents
<script src="login.js"></script>
<script src="postad.js"></script> //the script from which I want to get $SESSION_['user_id']
</body>
我希望此变量稍后插入到数据库中。有什么建议吗?
您可以将会话 ID 值分配给 JavaScript 变量,使其可用于 JavaScript:
<!DOCTYPE html>
<?php include 'connect.php';
session_start();
echo $_SESSION['user_id']; //works fine
//;
?>
<body>
//contents
<script>
var sessionId = "<?php echo $_SESSION['user_id'] ?>";
</script>
<script src="login.js"></script>
<script src="postad.js"></script> // "sessionId" variable should be available for the script
</body>
如果你想在外部 js 文件中调用 php 变量,那么你必须在输入中存储值。
Main.php
<body>
<input type="hidden" value="$SESSION_['user_id']" class="user_id">
<script src="login.js"></script>
<script src="postad.js"></script> //the script from which I want to get $SESSION_['user_id']
</body>
postad.js
$(function(){
var user_id = $('.user_id').val();
alert(user_id);
});
如果你想在同一个文件中使用,那么你可以直接使用 php echo.
Main.php
<body>
//content..
<script>
var user_id = "<?php echo $SESSION_['user_id'] ?>";
</script>
<script src="login.js"></script>
<script src="postad.js"></script> // console.log(user_id); that file you'll get that id.
</body>
我知道之前有人问过这个问题,但是从头到尾都没有解释。我有一个 login.php
,我想从中得到 $_SESSION['user_id']
来自
<?php
include "connect.php";
session_start();
$email=$_POST['email'];
$password=$_POST['password'];
$select_data=mysql_query("select * from user where email='$email' and password='$password'");
if(isset($_POST['email']))
{
if($row=mysql_fetch_array($select_data))
{
$_SESSION['user_id']=$row['id']; //The variable want to access from ext.js
echo "success";
}
else
{
echo "fail";
}
exit();
}
?>
顺便说一句,这个认证工作正常
我的Main.php
<!DOCTYPE html>
<?php include 'connect.php';
session_start();
echo $_SESSION['user_id']; //works fine
//;
?>
<body>
//contents
<script src="login.js"></script>
<script src="postad.js"></script> //the script from which I want to get $SESSION_['user_id']
</body>
我希望此变量稍后插入到数据库中。有什么建议吗?
您可以将会话 ID 值分配给 JavaScript 变量,使其可用于 JavaScript:
<!DOCTYPE html>
<?php include 'connect.php';
session_start();
echo $_SESSION['user_id']; //works fine
//;
?>
<body>
//contents
<script>
var sessionId = "<?php echo $_SESSION['user_id'] ?>";
</script>
<script src="login.js"></script>
<script src="postad.js"></script> // "sessionId" variable should be available for the script
</body>
如果你想在外部 js 文件中调用 php 变量,那么你必须在输入中存储值。
Main.php
<body>
<input type="hidden" value="$SESSION_['user_id']" class="user_id">
<script src="login.js"></script>
<script src="postad.js"></script> //the script from which I want to get $SESSION_['user_id']
</body>
postad.js
$(function(){
var user_id = $('.user_id').val();
alert(user_id);
});
如果你想在同一个文件中使用,那么你可以直接使用 php echo.
Main.php
<body>
//content..
<script>
var user_id = "<?php echo $SESSION_['user_id'] ?>";
</script>
<script src="login.js"></script>
<script src="postad.js"></script> // console.log(user_id); that file you'll get that id.
</body>