Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Test if user already has portal table entry in login page

    Scheduled Pinned Locked Moved Captive Portal
    4 Posts 2 Posters 841 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DerelictD
      Derelict LAYER 8 Netgate
      last edited by

      I have noticed that some users get their browser in a state that it thinks the target page is the portal page (:8002, etc).  When they log in they just get the portal page again.

      I am thinking about writing something to put at the top of the login page that tests to see if the user already has a portal entry and does something intelligent like "You are already logged in, click here" instead of simply displaying the login page again.

      Thought I'd ask if anyone has already written something like this before doing it myself.  Shouldn't be too difficult.

      Chattanooga, Tennessee, USA
      A comprehensive network diagram is worth 10,000 words and 15 conference calls.
      DO NOT set a source address/port in a port forward or firewall rule unless you KNOW you need it!
      Do Not Chat For Help! NO_WAN_EGRESS(TM)

      1 Reply Last reply Reply Quote 0
      • E
        EMWEE
        last edited by

        https://forum.pfsense.org/index.php?topic=77143.0

        Check this topic. User GertJan made a function that yiu can use. I use it for a manual logout page that uses a function to check if a user is already connected. If so the user gets redirected to a custom page.

        1 Reply Last reply Reply Quote 0
        • E
          EMWEE
          last edited by

          Example:

          captiveportal.inc

          Insert the next function above function portal_reply_page

          /* Use this function for MAC */
          function already_connected($clientmac) {
          	global $cpzone;
          
          	if ($clientmac != "") {
          		$query = "WHERE mac = '{$clientmac}'";
          	$cpdb = captiveportal_read_db($query);
          	/* Lookup the $sessionid */
          	foreach ($cpdb as $cpentry) {
          		if ($cpentry[3] == $clientmac)
          		return $cpentry[5];
          		}
          		return false;
          		} else
          	return false;
          	}	
          

          Replace with function portal_reply_page

          function portal_reply_page($redirurl, $type = null, $message = null, $clientmac = null, $clientip = null, $username = null, $password = null) {
                  global $g, $config, $cpzone;
          
                  /* Get captive portal layout */
                  if ($type == "redir") {
                          header("Location: {$redirurl}");
                          return;
                  } else if ($type == "login")
                          $htmltext = get_include_contents("{$g['varetc_path']}/captiveportal_{$cpzone}.html");
                  else if ($type == "already_connected") {
          
          				/* Enable for /MAC use */
          				$sessionid = already_connected($clientmac);
          
                          $htmltext = get_include_contents("{$g['captiveportal_path']}/captiveportal-{$cpzone}-already-connected.html");
                  } else
                          $htmltext = get_include_contents("{$g['varetc_path']}/captiveportal-{$cpzone}-error.html");
          
                  $cpcfg = $config['captiveportal'][$cpzone];
          
                  /* substitute the PORTAL_REDIRURL variable */
                  if ($cpcfg['preauthurl']) {
                          $htmltext = str_replace("\$PORTAL_REDIRURL\$", "{$cpcfg['preauthurl']}", $htmltext);
                          $htmltext = str_replace("#PORTAL_REDIRURL#", "{$cpcfg['preauthurl']}", $htmltext);
                  }
          
                  /* substitute other variables */
                  $ourhostname = portal_hostname_from_client_ip($clientip);
                  $protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
                  $htmltext = str_replace("\$PORTAL_ACTION\$", "{$protocol}{$ourhostname}/", $htmltext);
                  $htmltext = str_replace("#PORTAL_ACTION#", "{$protocol}{$ourhostname}/", $htmltext);
          
                  $htmltext = str_replace("\$PORTAL_ZONE\$", htmlspecialchars($cpzone), $htmltext);
                  $htmltext = str_replace("\$PORTAL_REDIRURL\$", htmlspecialchars($redirurl), $htmltext);
                  $htmltext = str_replace("\$PORTAL_MESSAGE\$", htmlspecialchars($message), $htmltext);
                  $htmltext = str_replace("\$CLIENT_MAC\$", htmlspecialchars($clientmac), $htmltext);
                  $htmltext = str_replace("\$CLIENT_IP\$", htmlspecialchars($clientip), $htmltext);
                  $htmltext = str_replace("\$PORTAL_SESSION\$", htmlspecialchars($sessionid), $htmltext);
          
                  // Special handling case for captive portal master page so that it can be ran
                  // through the PHP interpreter using the include method above.  We convert the
                  // $VARIABLE$ case to #VARIABLE# in /etc/inc/captiveportal.inc before writing out.
                  $htmltext = str_replace("#PORTAL_ZONE#", htmlspecialchars($cpzone), $htmltext);
                  $htmltext = str_replace("#PORTAL_REDIRURL#", htmlspecialchars($redirurl), $htmltext);
                  $htmltext = str_replace("#PORTAL_MESSAGE#", htmlspecialchars($message), $htmltext);
                  $htmltext = str_replace("#CLIENT_MAC#", htmlspecialchars($clientmac), $htmltext);
                  $htmltext = str_replace("#CLIENT_IP#", htmlspecialchars($clientip), $htmltext);
                  $htmltext = str_replace("#PORTAL_SESSION#", htmlspecialchars($sessionid), $htmltext);
                  $htmltext = str_replace("#USERNAME#", htmlspecialchars($username), $htmltext);
                  $htmltext = str_replace("#PASSWORD#", htmlspecialchars($password), $htmltext);
          
                  echo $htmltext;
          }
          

          Upload a custom $zone-already-connected.html

          Insert in index.php after line :

          portal_allow($clientip, $clientmac, "unauthenticated");

          		/* Use this for MAC */
           else if (already_connected($clientmac)) { 
          	/* display already connected page */
          	portal_reply_page($redirurl, "already_connected",null,$clientmac,$clientip); 
          

          All credits to GertJan

          1 Reply Last reply Reply Quote 0
          • DerelictD
            Derelict LAYER 8 Netgate
            last edited by

            Perfect.  Thanks.

            I'm already using a custom portal_reply_page() and index.php.  Ought to be a piece of cake.

            Chattanooga, Tennessee, USA
            A comprehensive network diagram is worth 10,000 words and 15 conference calls.
            DO NOT set a source address/port in a port forward or firewall rule unless you KNOW you need it!
            Do Not Chat For Help! NO_WAN_EGRESS(TM)

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.