全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 1411|回复: 17
打印 上一主题 下一主题

[翻译] 请教大佬一个php的问题【已解决】

[复制链接]
跳转到指定楼层
1#
发表于 2019-11-4 16:21:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 003 于 2019-11-4 17:58 编辑

我想打开网站,未登录的用户跳转到注册/登录页,注册/登录后才可以跳转到首页。

我在index.php顶部加了一段:

  1. <?php
  2. //未登录,跳转至欢迎界面
  3. if (!isset($_SESSION['user'])) {
  4.   header('location:login.php');
  5.   exit();   //防止继续执行
  6. }
  7. ?>
复制代码


然后在注册登录页顶部加了一段:

  1. <?php
  2.     session_start();    //首先要开始 session
  3.     //已登录,跳转至主页
  4.   if (isset($_SESSION['user'])) {
  5.     header('location:index.php');
  6.     exit();
  7.   }
  8. ?>
复制代码


效果是,打开网站能跳转到注册/登录页,但是输入账号密码后,就提示,该网页无**常运作,将您重定向的次数过多。


请问大佬,这是啥原因啊?
来自 11#
发表于 2019-11-4 17:48:13 | 只看该作者
本帖最后由 后悔当初不努力 于 2019-11-4 17:49 编辑



这一行
loginCookie($helloConfig['ppsecure'],$userInfo['uid'],$userInfo['nickname'],$userInfo['groupid']);

上下都可以 添加 $_SESSION['user'] = $userInfo['uid'];   
然后index.php 头部也加session_start();

  这个看起来人家是有用cookie判断登录了,$loginInfo['uid']>0 就是判断是否登录成功的  
  index.php 肯定没有$_SESSION['user'] 的 所以会跳回登录   但是人家原来的代码有判断已经登录了
  最后的办法就是 不用用你家的session判断  直接 index.php
开头
<?php
require(dirname(__FILE__)."/global.php");
if ( !$loginInfo['uid'] > 0 )
                        {
                               //跳去登录
                        }
2#
发表于 2019-11-4 16:24:04 来自手机 | 只看该作者
因为你登陆完没设置$_SESSION['user']
3#
 楼主| 发表于 2019-11-4 16:28:17 | 只看该作者
本帖最后由 003 于 2019-11-4 16:31 编辑
TechBlack 发表于 2019-11-4 16:24
因为你登陆完没设置$_SESSION['user']


怎么设置啊?大佬,可以的话,zhifu报私我,给你发个红包
5#
发表于 2019-11-4 16:33:20 来自手机 | 只看该作者
login.php里面登陆成功里面加一行$_SESSION['user']=$你的用户名变量
6#
发表于 2019-11-4 17:21:16 | 只看该作者
index.php页面 参照 login.php 页面
添加一个 session_start();

注意位置
7#
 楼主| 发表于 2019-11-4 17:28:22 | 只看该作者
.host. 发表于 2019-11-4 17:21
index.php页面 参照 login.php 页面
添加一个 session_start();

还是不行。。。就像2楼大佬说的,那个加一行一行$_SESSION['user']=$你的用户名变量,不知道在哪加。。
8#
发表于 2019-11-4 17:32:31 | 只看该作者
登录成功后的那个header跳转之前  设置$_SESSION['user']=你的用户名
但是如果你没设置的话 不可能跳来跳去的  只会重新跳回login.php让你再登录。只有你设置了,才有可能一直跳来跳去重定向太多,如果设置了,那需要在index.php的第二行加上 session_start();
9#
发表于 2019-11-4 17:33:33 | 只看该作者
后悔当初不努力 发表于 2019-11-4 17:32
登录成功后的那个header跳转之前  设置$_SESSION['user']=你的用户名
但是如果你没设置的话 不可能跳来跳去 ...

你把login.php的完整代码贴上来吧
10#
 楼主| 发表于 2019-11-4 17:39:52 | 只看该作者
后悔当初不努力 发表于 2019-11-4 17:33
你把login.php的完整代码贴上来吧
  1. <?php

  2. require(dirname(__FILE__)."/global.php");

  3. if ( isset($_GET['do']) )
  4. {
  5.         if ( $_GET['do'] == "Login" )
  6.         {
  7.                 if( isset($_POST['account'],$_POST['password']) )
  8.                 {
  9.                         if ( $loginInfo['uid'] > 0 )
  10.                         {
  11.                                 die('{"result":"login","message":""}');
  12.                         }

  13.                         $loginAccount        = strtolower(stripslashes(trim($_POST['account'])));

  14.                         $loginPassword        = stripslashes($_POST['password']);

  15.                         if( strlen($loginAccount) < 2 )
  16.                         {
  17.                                 die('{"result":"error","message":"账号无效","position":1}');
  18.                         }
  19.                                
  20.                         if( strlen($loginPassword) < 6 || strlen($loginPassword) > 26 || substr_count($loginPassword," ") > 0 )
  21.                         {
  22.                                 die('{"result":"error","message":"密码无效","position":2}');
  23.                         }

  24.                         $loginType = "nickname";

  25.                         if( emailCheck($loginAccount) )
  26.                         {
  27.                                 $loginType = "email";
  28.                         }
  29.                         else
  30.                         {
  31.                                 if( checkNickname($loginAccount) != "" )
  32.                                 {
  33.                                         die('{"result":"error","message":"账号不合法","position":1}');
  34.                                 }
  35.                         }

  36.                         $DB = database();

  37.                         $userInfo = hello::getMemberInfo($DB,$loginType,$loginAccount);

  38.                         if( empty($userInfo['uid']) )
  39.                         {
  40.                                 echo '{"result":"error","message":"账号不存在","position":1}';
  41.                         }
  42.                         else
  43.                         {
  44.                                 if( md5($loginPassword) == $userInfo['password'] )
  45.                                 {
  46.                                         loginCookie($helloConfig['ppsecure'],$userInfo['uid'],$userInfo['nickname'],$userInfo['groupid']);

  47.                                         echo '{"result":"success","message":"登录成功","position":0}';
  48.                                 }
  49.                                 else
  50.                                 {
  51.                                         if( $userInfo['password'] == "" )
  52.                                         {
  53.                                                 echo '{"result":"error","message":"该账号不支持密码登录","position":2}';
  54.                                         }
  55.                                         else
  56.                                         {
  57.                                                 echo '{"result":"error","message":"账号与密码不匹配","position":2}';
  58.                                         }
  59.                                 }
  60.                         }

  61.                         $DB->close();
  62.                 }
  63.         }
  64.         else if ( $_GET['do'] == "logout" )
  65.         {
  66.                 singOut();

  67.                 header("location:".$_SERVER['PHP_SELF']);
  68.         }
  69.         else if ( $_GET['do'] == "SignUp" )
  70.         {
  71.                 if( isset($_POST['email'],$_POST['nickname'],$_POST['password']) )
  72.                 {
  73.                         if ( $loginInfo['uid'] > 0 )
  74.                         {
  75.                                 die('{"result":"login","message":""}');
  76.                         }

  77.                         if ( !$helloConfig['emailjoin'] )
  78.                         {
  79.                                 die('{"result":"error","message":"您暂时不能使用邮箱注册","position":0}');
  80.                         }                       

  81.                         $email        = strtolower(stripslashes(trim($_POST['email'])));

  82.                         $nickname = filterCode($_POST['nickname'],true);

  83.                         $password        = stripslashes($_POST['password']);

  84.                         if( !emailCheck($email) )
  85.                         {
  86.                                 die('{"result":"error","message":"邮件地址不正确","position":1}');
  87.                         }

  88.                         $nicknameError = checkNickname($nickname);

  89.                         if( $nicknameError != "" )
  90.                         {
  91.                                 die('{"result":"error","message":"'.$nicknameError.'","position":2}');
  92.                         }

  93.                         if( substr_count($password," ") > 0 )
  94.                         {
  95.                                 die('{"result":"error","message":"密码不能使用空格","position":3}');
  96.                         }

  97.                         if( strlen($password) < 6 || strlen($password) > 26 )
  98.                         {
  99.                                 die('{"result":"error","message":"密码长度不合法","position":3}');
  100.                         }

  101.                         $DB = database();

  102.                         if( hello::getMemberCount($DB,"email",$email) != 0 )
  103.                         {
  104.                                 echo '{"result":"error","message":"邮件地址已被占用","position":1}';
  105.                         }
  106.                         else
  107.                         {
  108.                                 if( hello::getMemberCount($DB,"nickname",$nickname) != 0 )
  109.                                 {
  110.                                         echo '{"result":"error","message":"昵称已被占用","position":2}';
  111.                                 }
  112.                                 else
  113.                                 {
  114.                                         $userID = hello::memberJoin($DB,$nickname,$email,md5($password),"","",$coinConfig['register']);

  115.                                         if ($userID > 0)
  116.                                         {
  117.                                                 newAvatar($userID,"");

  118.                                                 loginCookie($helloConfig['ppsecure'],$userID,$nickname,1);

  119.                                                 echo '{"result":"success","message":"注册成功"}';
  120.                                         }
  121.                                         else
  122.                                         {
  123.                                                 echo '{"result":"error","message":"注册失败","position":0}';
  124.                                         }
  125.                                 }
  126.                         }

  127.                         $DB->close();
  128.                 }
  129.         }
  130.         else if ( $_GET['do'] == "sendPassword" )
  131.         {
  132.                 if( isset($_POST['email'],$_POST['verify_code']) )
  133.                 {
  134.                         session_start();

  135.                         $sessionCode = isset($_SESSION['identifying_code']) ? $_SESSION['identifying_code'] : "";

  136.                         session_destroy();

  137.                         if( $sessionCode == "" || $sessionCode != md5(strtoupper($_POST['verify_code'])) )
  138.                         {
  139.                                 die('{"result":"error","message":"验证码不正确","position":2}');
  140.                         }

  141.                         if( !emailCheck($_POST['email']) )
  142.                         {
  143.                                 die('{"result":"error","message":"邮件地址不合法","position":1}');
  144.                         }

  145.                         $DB = database();

  146.                         $userInfo = hello::getMemberInfo($DB,"email",$_POST['email']);

  147.                         if( $userInfo['email'] == "" )
  148.                         {
  149.                                 echo '{"result":"error","message":"该邮件地址尚未登记","position":1}';
  150.                         }
  151.                         else
  152.                         {
  153.                                 $codeArr = hello::getResetPasswordCode($DB,"uid",$userInfo['uid']);

  154.                                 $resetCode = $codeArr['code'];

  155.                                 if( time() - $codeArr['dateline'] >= 1800 )
  156.                                 {
  157.                                         $resetCode = $userInfo['uid'].createSecureKey(1,false).createSecureKey(9);

  158.                                         $DB->query("REPLACE INTO `hello_resetpassword` VALUE(".$userInfo['uid'].",'".$resetCode."',".time().")");
  159.                                 }

  160.                                 if( time() - $codeArr['dateline'] > 120 )
  161.                                 {
  162.                                         if( $codeArr['dateline'] > 0 )
  163.                                         {
  164.                                                 $DB->query("UPDATE `hello_resetpassword` SET `dateline`=".time()." WHERE `uid`=".$userInfo['uid']);
  165.                                         }

  166.                                         sendPasswordEmail($helloConfig['sitename'],$helloConfig['sitemail'],$userInfo['email'],$userInfo['nickname'],$resetCode);
  167.                                 }

  168.                                 echo '{"result":"success","message":""}';
  169.                         }

  170.                         $DB->close();
  171.                 }
  172.         }
  173.         else if ( $_GET['do'] == "resetPassword" )
  174.         {
  175.                 if( isset($_GET['code'],$_POST['password']) )
  176.                 {
  177.                         $newPassword = stripslashes($_POST['password']);

  178.                         if( substr_count($newPassword," ") > 0 )
  179.                         {
  180.                                 die('{"result":"error","message":"密码不能使用空格"}');
  181.                         }

  182.                         if( strlen($newPassword) < 6 || strlen($newPassword) > 26 )
  183.                         {
  184.                                 die('{"result":"error","message":"密码长度不合法"}');
  185.                         }

  186.                         $DB = database();

  187.                         $codeArr = hello::getResetPasswordCode($DB,"code",strAddslashes($_GET['code']));

  188.                         if( empty($codeArr['uid']) || time() - $codeArr['dateline'] > 2000 )
  189.                         {
  190.                                 echo '{"result":"error","message":"链接已失效,请刷新页面"}';
  191.                         }
  192.                         else
  193.                         {
  194.                                 $DB->query("UPDATE `hello_member` SET password='".md5($newPassword)."' WHERE `uid`=".$codeArr['uid']);

  195.                                 $DB->query("DELETE FROM `hello_resetpassword` WHERE `uid`=".$codeArr['uid']);

  196.                                 $userInfo = hello::getMemberInfo($DB,"uid",$codeArr['uid']);

  197.                                 loginCookie($helloConfig['ppsecure'],$userInfo['uid'],$userInfo['nickname'],$userInfo['groupid']);

  198.                                 echo '{"result":"success","message":""}';
  199.                         }

  200.                         $DB->close();
  201.                 }
  202.         }
  203.         else if ( $_GET['do'] == "password" )
  204.         {
  205.                 if( $loginInfo['uid'] > 0 )
  206.                 {
  207.                         header("location:./");

  208.                         exit;
  209.                 }

  210.                 $resetCode = "";

  211.                 if( isset($_GET['code']) )
  212.                 {
  213.                         if( $_GET['code'] != "" )
  214.                         {
  215.                                 $DB = database();

  216.                                 $codeArray = hello::getResetPasswordCode($DB,"code",strAddslashes($_GET['code']));

  217.                                 $DB->close();

  218.                                 if( time() - $codeArray['dateline'] < 1800 )
  219.                                 {
  220.                                         $resetCode = $codeArray['code'];
  221.                                 }
  222.                         }

  223.                         if( $resetCode == "" )
  224.                         {
  225.                                 header("location:./passport.php?do=password");

  226.                                 exit;
  227.                         }
  228.                 }

  229.                 $template = template( $isMobileRequest ? "mobile_reset_password.html" : "reset_password.html" );

  230.                 $template->assign( 'helloConfig', $helloConfig );

  231.                 $template->assign( 'loginInfo', $loginInfo );

  232.                 if( !$isMobileRequest )
  233.                 {
  234.                         $template->assign( 'headerNavi', "passport" );
  235.                 }

  236.                 $template->assign( 'resetCode', $resetCode );

  237.                 $template->output();
  238.         }
  239.         else if ( $_GET['do'] == "register" )
  240.         {
  241.                 if( $loginInfo['uid'] > 0 || !$helloConfig['emailjoin'] )
  242.                 {
  243.                         header("location:".$_SERVER['PHP_SELF']);
  244.                 }
  245.                 else
  246.                 {
  247.                         $template = template( $isMobileRequest ? "mobile_register.html" : "register.html" );

  248.                         $template->assign( 'helloConfig', $helloConfig );

  249.                         $template->assign( 'loginInfo', $loginInfo );

  250.                         if( !$isMobileRequest )
  251.                         {
  252.                                 $template->assign( 'headerNavi', "passport" );
  253.                         }

  254.                         $template->output();
  255.                 }
  256.         }
  257.         else
  258.         {
  259.                 header("location:".$_SERVER['PHP_SELF']);
  260.         }
  261. }
  262. else
  263. {
  264.         if ( $loginInfo['uid'] > 0 )
  265.         {
  266.                 $locationURL = "./";
  267.                
  268.                 if( isset($_COOKIE['returnURL']) )
  269.                 {
  270.                         if( substr($_COOKIE['returnURL'], 0 ,1) == "/" )
  271.                         {
  272.                                 $locationURL = $_COOKIE['returnURL'];
  273.                         }

  274.                         setcookie( 'returnURL', '', 0, "/" );
  275.                 }
  276.                
  277.                 header("location:".$locationURL);
  278.         }
  279.         else
  280.         {
  281.                 if( isset($_GET['return']) )
  282.                 {
  283.                         if( substr($_GET['return'], 0 ,1) == "/" )
  284.                         {
  285.                                 setcookie( 'returnURL', $_GET['return'], time()+3600, "/" );
  286.                         }
  287.                 }

  288.                 $template = template( $isMobileRequest ? "mobile_login.html" : "login.html" );

  289.                 if( $isMobileRequest )
  290.                 {
  291.                         $userAgent =  isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";

  292.                         if( strpos($userAgent, 'MicroMessenger') === false )
  293.                         {
  294.                                 $helloConfig['weixinjoin'] = 0;
  295.                         }
  296.                 }
  297.                 else
  298.                 {
  299.                         $template->assign( 'headerNavi', "passport" );
  300.                 }
  301.                
  302.                 $template->assign( 'helloConfig', $helloConfig );

  303.                 $template->assign( 'loginInfo', $loginInfo );

  304.                 $template->assign( 'connectArray', isConnect($helloConfig['ppsecure']) );

  305.                 $template->output();
  306.         }
  307. }
  308. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-1-15 19:27 , Processed in 0.064260 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表