我有一个网站,用户可以登录。我试过:
<?php echo $_SESSION ['userlogin']
当用户登录时,我将他们的 session 设置为 userlogin,但它不会显示他们的用户名。
This is the tutorial I used
最佳答案
你有没有start the session在使用之前?
所以要设置它:
<?php
//Fetch the username from the database
//The $login and $password I use here are examples. You should substitute this
//query with one that matches your needs and variables.
//On top of that I ASSUMED you are storing your passwords MD5 encrypted. If not,
//simply remove the md5() function from below.
$query = "SELECT name FROM users WHERE login='" . mysql_real_escape_string($login) . "' AND password='" . md5($password) . "'";
$result = mysql_query($query);
//Check if any row was returned. If so, fetch the name from that row
if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_assoc_assoc($result);
$name = $row['name'];
//Start your session
session_start();
//Store the name in the session
$_SESSION['userlogin'] = $name;
}
else {
echo "The combination of the login and password do not match".
}
?>
要在另一个页面上检索它,请执行以下操作:
<?php
//Start your session
session_start();
//Read your session (if it is set)
if (isset($_SESSION['userlogin']))
echo $_SESSION['userlogin'];
?>
编辑
有关如何创建登录表单的更多信息。您说您尝试设置 $_SESSION['user']但这是行不通的。
所以只要确保你确实做了 session_start();在那之前。如果你这样做了,一切都应该有效。除非您为 session 分配一个空变量。因此,请仔细检查您分配的变量是否确实包含一个值。喜欢:
<?php
session_start();
echo "Assigning session value to: " . $user;
$_SESSION['user'] = $user;
?>
在您将我链接到他们正在做的教程中:
$_SESSION[user]=$_GET[userlogin];
这意味着他们正在分配从他们在此处创建的登录表单中获得的值:
function loginform() {
print "please enter your login information to proceed with our site";
print ("<table border='2'><tr><td>username</td><td><input type='text' name='userlogin' size'20'></td></tr><tr><td>password</td><td><input type='password' name='password' size'20'></td></tr></table>");
print "<input type='submit' >";
print "<h3><a href='registerform.php'>register now!</a></h3>";
}
你看<input type='text' name='userlogin' size'20'> .但是没有<form></form>在此表单周围标记.. 所以这不会正确发布。所以你应该做的是:
<form method="POST" action="index.php">
<label for="userlogin">Username:</label> <input type="text" id="userlogin" name="userlogin" size="20" />
<label for="password">Password:</label> <input type="password" id="password" name="password" size="20" />
<input type="submit" value="login" />
</form>
此表单会将表单发送回 index.php,其中 userlogin 和 password 为 $_POST变量。
在您的 index.php 中,您可以执行以下操作:
<?php
//Get variables:
$login = mysql_real_escape_string($_POST['userlogin']);
$pass = mysql_real_escape_string($_POST['password']);
//Check your table:
$query = "SELECT userlogin FROM users WHERE userlogin = '" . $login . "' AND password='" . $pass . "'";
$result = mysql_query($query);
//Check if this user exists:
if (mysql_num_rows($result) == 1) {
echo "User exists!";
//Store the login in the session:
session_start();
$_SESSION['userlogin'] = $login;
}
else {
echo "Unknown user";
}
?>
如果不为您编写完整的代码,我再清楚不过了。所以我希望这对你有所帮助。
关于php - 如何在 php 中获取 session ID 或用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8703507/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式rubyshell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R