Jump to content

wierd url behaviour


wilsea
 Share

Recommended Posts

I seem to have created some sort of chaos for one of my url links, but I am totally lost for how to sort it out.

I have a url in a header menu bar which is supposed to go to  processMaths2/teacheradmin/teachers/tom/2c/studentname .  

However clicking on the link adds an unwanted id=2983 to the url (same number regardless of student)
http://localhost:81/processMaths2/teacheradmin/teachers/tom/2c/harry/id=2983
 
This is a meaningless url and although I would expect a 404, I actually end up in the localhost home page (ie out of processwire completely). 

The actual html for the link in on the page looks fine (and displays normally at the bottom of the web page when I hover over the link)
<a href="/processMaths2/teacheradmin/teachers/tom/2c/harry/">View Results</a>
 

If I go to the page by viewing it from admin - it works fine. - the url is   http://localhost:81/processMaths2/teacheradmin/teachers/tom/2c/harry/

There's nothing too exotic about the code generating the url ......the first three echos are for debugging and display exactly what I expect.  

if ($role->name === "student") {
        $studentPage = $pages->get("template=student,user_id=$user->id"); 
        echo "Game ID" .($studentPage->game_id);
        echo "Student Page ID" . $studentPage->id;
        echo "Role" . $role->name;
        $tempGame = $pages->get("template=game,student_id=000");
        $gamePage = ($studentPage->game_id > 0)?  $pages->get("template=game,id=$studentPage->game_id") : $tempGame;
       
        $admin = '<a href="' . $studentPage->url . '">View Results</a>';
        $account = '<a href="' . $gamePage->url . '">Play Game</a>'; 
        if($page->template =="game") $account = "";
        if($page->template =="student") $admin = "";
               
    }
echo $account . $admin
So the $admin link is crazy, but the $account link works fine.
 
The mystery id=2983 is not related to either the game_id or the student_id - it is however the id of one of the teacher pages. I have no idea where it is coming from. It is only added to $admin, but not to $account.  The joys of programming!
 
Can anyone give me some ideas about what to do next to sort out the problem? 
Link to comment
Share on other sites

Thanks Adrian there are indeed some redirects going on, though not in the student page.  Some of them could be a bit dodgy.

I'd check it out, but I'm now having problems getting apache going. 

I thought I'd try reinstalling wamp, since it's normally a breeze to do and I was suspicious that it was taking a long time to load, and all pages - even those with straight html, were showing up as still loading.

Now I haven't been able to come up with an apache configuration that works. Wamp 2.5 requires manual setting up of a virtual host for each project (in http.conf and in  etc/hosts ) and I still couldn't  get beyond the home page in any processwire project ( eg skyscapers) . 

I tried reinstalling  Wampserver 2.2 to avoid the manual setup pain and but now I get a forbidden for localhost:81.  

So I guess I'll try Wamp 2.5 again and persevere with getting it going. 

Alternative suggestions, configuration ideas most welcome.

Link to comment
Share on other sites

I've been a long time fan of The Uniform Server. No installation, no registry settings. It just unpacks to a folder and is ready to go. You can setup vhosts from within the controller program, including editing the windows hosts file. You could just put it on a USB stick to always have a wamp environment with you.

  • Like 2
Link to comment
Share on other sites

@SiNNut

The Uniform Server is an absolute gem!   :)  Felt like it only took 30 seconds to setup! Very sweet.

Now I can get back to programming with processwire! 

Thanks a million!

Link to comment
Share on other sites

Now that I have a server (Uniform Server)  up and going again (and recovered from PW withdrawal symptoms) ........

I've checked for redirects, and can't find any that look like a problem. I have redirects from my login page  based on $user->role - they all work fine.

The problem link is on a template game.php, which doesn't have any redirects on it.

<?php
/*
 * create initial variables for game - these are passed to html page as json string * 
 * then display a game setup with appropriate values for logged in player  
 * 
 */

$student = $pages->get('template=student,user_id=' . $user->id);
$pageInfo = "UserId =  $user->id :StudentID = $student->id : UserRole =  $user->roles";
$pageInfo.= " :StudentName = $student->title ";
$pageInfo.= " :Game =$page->name :template = $page->template ";

// create a variable to pass to javascript as json string
$data = new stdClass();
$data->user_id = $student->id;
$data->user_name = $student->first_name . " " . $student->last_name;

if (!empty($student->game_id) || $student->game_id <> 0) {
    $data->gameId = $student->game_id;

    // get game details
    $game = wire("pages")->get($student->game_id);
    $data->question_list = $game->question_list;
    $data->list_pos = $game->list_pos;

} else {

    // set a value that triggers generating a new game
    $data->gameId = 0;
    $data->level_id = 0;
}

// add level data if it exists
if (strlen($student->sequence) > 1) {
    $levels = explode(",", $student->sequence);
    $data->level_id = $levels[0];
    $level = $pages->get('template=level,id=' . $levels[0]);
    foreach ($level->fields as $field) {
        $data->{$field->name} = $level->get($field->name);
    }
}

//----------------------page presentation  ------------------------------------------------------

$config->styles->add($config->urls->templates . 'styles/main.css');
//$config->styles->add($config->urls->templates . 'styles/theme.css');

// if current template has it's own custom CSS file, then include it
$file = "styles/{$page->template}.css";
if (is_file($config->paths->templates . $file))
    $config->styles->add($config->urls->templates . $file);

// add our javascripts
//$config->scripts->prepend('http://code.jquery.com/jquery-1.10.2.min.js');
$config->scripts->add($config->urls->templates . 'scripts/main.js');
$config->scripts->add($config->urls->templates . 'scripts/mathGame.js');
$config->scripts->prepend($config->urls->templates . 'scripts/jquery-1.10.2.min.js');

// if current template has it's own custom JS file, then include it
$file = "scripts/{$page->template}.js";
if (is_file($config->paths->templates . $file))
    $config->scripts->add($config->urls->templates . $file);
?>
<html>
<head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title><?php echo $page->title ?></title>
    <?php
    foreach ($config->styles as $url)
        echo "<link rel='stylesheet' type='text/css' href='$url' />";
    foreach ($config->scripts as $url)
        echo "<script type='text/javascript' src='$url'></script>";
    ?>

    <script>
    //            pass variables from server php to javascript
        gameId = <?php echo $page->id ?>;
        loadedGame = <?php echo json_encode($data) . "; \r\n" ?>
    </script>

</head>
<body>
    <div> <?php  echo $pageInfo;   ?> </div>
    <div id="debug" >...</div>   
    <div> Welcome <?php echo $user->name, $session->id ?> </div>
    <div id="topNav">          
        <?php include ('./includes/topNav.inc'); ?>
    </div>

    <!--===============================================================================================================-->
    <div id="progress"> <img src ="<?php echo $config->urls->templates ?>/styles/images/glassy-smiley-good.png ?>"  height ="50"  width = "50" /> </div>

    <!--======================================= MAIN ====================================================================-->
    <div id="main" >

        <table><tr>
            <!--=========================================  NUMPAD   =====================================================-->
            <td>
                <div id="numContainer" class="left">

                    <div id ="question"></div>
                    <!--<div id="feedbackContainer" > <div id ="feedback"> <img src ="images/glassy-smiley-good.png" height ="30" width = "30"> </div> </div>-->      

                    <div id ="answerDisplay">
                        <div id="answerInner"> <span id="a0" class="ad"></span><span id="a1" class="ad"></span><span class="ad" id='a2' ></span><span class="ad"></span></div>
                        <img id="feedback" src ="<?php echo $config->urls->templates ?>styles/images/glassy-smiley-good.png ?>" height ="30" width = "30"> 
                    </div>

                    <div id="roundResult"></div>

                    <div id="numpadContainer">
                        <span id="anim"></span>

                        <table id ="numpad" border ="1">

                            <tr><td><button class ="numbtn numpad">7</button></td>
                                <td><button class ="numbtn numpad">8</button></td>
                                <td><button class ="numbtn numpad">9</button></td></tr>

                            <tr><td><button class ="numbtn numpad">4</button></td>
                                <td><button class ="numbtn numpad">5</button></td>
                                <td><button class ="numbtn numpad">6</button></td></tr>

                            <tr><td><button class ="numbtn numpad">1</button></td>
                                <td><button class ="numbtn numpad">2</button></td>
                                <td><button class ="numbtn numpad">3</button></td></tr>

                            <tr><td></td>
                                <td><button  class ="numbtn numpad">0</button></td>
                                <td> <img id="eraser" src ="<?php echo $config->urls->templates ?>styles/images/eraser.jpg ?>" height ="80" width = "80"> </td><tr>

                        </table>
                    </div>

                    <button id="start"><img id="eraser" src ="<?php echo $config->urls->templates ?>styles/images/start.jpg ?>" height ="50" width = "200"><br />START</button>
                </div>
            </td>
            <!--=========================================  feedback   =====================================================-->
            <td id="display">
                Round List Pos:<div id ='roundPos'>List Pos</div><br />
                All List Pos:<div id ='allPos'>All Pos</div><br />
                <div id="php" ></div>
                <div id ="announce"><img src ="<?php echo $config->urls->templates ?>styles/images/glassy-smiley-good.png"></div>
            </td>
        </tr></table>
   </div>

</body>
</html>

It does load a game.js file, which does use ajax to determine a window location if a new game is started . The original code just relied on the right url coming back from the server and going to that page with a window.location - I've now added some error catching to trap any problems, but it was actually working OK without the error trapping. 

$.ajax({
        type: "POST",
        url: "/processMaths2/backend/ajax-game/",
        data: game,
        success: function(rtndata) {
            $('#debug').html(rtndata);
            try{
              result =  $.parseJSON(rtndata)
              window.location.href = result.gameUrl;
            }
            else{
                alert("Problem with parsing ajax result")
            }            
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        }
    })

The student page which is never being reached via the link doesn't have any redirects - it just displays lots of data. When reached from other pages other than via the problem link, there is no problem.

<?php

$config->styles->add($config->urls->templates . 'skeleton/stylesheets/layout.css');
$config->styles->add($config->urls->templates . 'styles/main.css');
$config->styles->add($config->urls->templates . 'styles/level.css');
$config->styles->add($config->urls->templates . 'styles/theme.css');

// if current template has it's own custom CSS file, then include it
$file = "styles/{$page->template}.css";
if (is_file($config->paths->templates . $file))
    $config->styles->add($config->urls->templates . $file);

// add our javascripts
//$config->scripts->prepend('http://code.jquery.com/jquery-1.10.2.min.js');
$config->scripts->add($config->urls->templates . 'scripts/main.js');
$config->scripts->prepend($config->urls->templates . 'scripts/jquery-1.10.2.min.js');
$config->scripts->add($config->urls->templates . 'scripts/jquery.tablesorter.min.js');

// if current template has it's own custom JS file, then include it
$file = "scripts/{$page->template}.js";
if (is_file($config->paths->templates . $file))
    $config->scripts->add($config->urls->templates . $file);
?>
<html>
    <head>

        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title><?php echo $page->browser->title ?></title>
        <?php
        foreach ($config->styles as $url)
            echo "<link rel='stylesheet' type='text/css' href='$url' />";
        foreach ($config->scripts as $url)
            echo "<script type='text/javascript' src='$url'></script>";
        ?>

    </head>
    <body>
        <?php echo $page->friendlyTitle; ?>
        <?php echo $user->role, $user->name ?>
        <div id="topNav">

            <?php include ('./includes/topNav.inc'); ?>
            <!--       Top nav bar here-->

        </div>
        <div class="tabs">
            <ul>
                <li id="tabHeader_1">Current Game Grid</li>
                <li id="tabHeader_2">Current Game Table</li>  
                <li id="tabHeader_3">History Grid</li>  
                <li id="tabHeader_4">History Table</li>  
                <li id="tabHeader_5">Charts</li>
                <li id="tabHeader_6">Summary</li>                 
               
            </ul>
        </div>
        <div id="debug">...</div>

        <div id='tabContent'>

            <div id='tabContent_1'  class="content">  <?php echo $page->get('game_grid') ?></div>
            <div id='tabContent_2' class="content" > <?php include("includes/s_current_game.inc") ?> </div> 
            <div id='tabContent_3' class="content" > <?php echo $page->get('history_grid') ?> </div>  
            <div id='tabContent_4' class="content" > <?php include("includes/s_history.inc") ?> </div>
            <div id='tabContent_5' class="content" > <?php echo $page->get('charts') ?></div> 
            <div id='tabContent_6'  class="content"> <?php echo $page->get('summary') ?></div> 

        </div> <!--  end tabContent  ----->
        <div id='main'><?php echo $page->get('outMain|body'); // use outMain if there, or body otherwise    ?></div>
        <div id='side'><?php echo $page->get('outSide|sidebar'); // use outSide if there, or sidebar otherwise    ?></div>

    </body>
</html>

student.php  uses student.js to move from tab to tab, and display and sort tables of results. - no window.location anywhere.

Obviously the id=2983 must be coming from somewhere, but I can't figure out where. 

Link to comment
Share on other sites

The actual html for the link in on the page looks fine (and displays normally at the bottom of the web page when I hover over the link)

<a href="/processMaths2/teacheradmin/teachers/tom/2c/harry/">View Results</a>

Based on this and the fact that there are no redirects, the only thing I can think of is an apache rewrite. Are you using the standard one that comes with PW?  This seems unlikely given that your other pages are working fine. Do you have any modules installed, or custom hooks running?

Have you tried a fresh PW install, or at least copied over a new version of wire, .htaccess and index.php ?

EDIT:

Looking at your code and description of the problem I am getting more confused. Am I right in thinking it must be from: window.location.href = result.gameUrl;

What do you get if you:

console.log(result.gameURL);
Link to comment
Share on other sites

I get confused when I look at my code too.  :rolleyes:  I try to keep it clean, but it's easier said than done.

The ajax returned result is    {gameUrl: "http://localhost:81/processMaths2/games/g_2098_2998_3069/"} , so that's working exactly as it should  (once the try catch block was tidied up). It was at the top of my list for likely suspects once you mentioned redirects.

The only other redirects are from the logon page, a slightly modified version of an example found on the forum - they work as expected as well. - to teacher admin for user->role = "teacher", to play game for user->role = "student". 

<?php
/*
 * Basic login 
 * 
 */

function getUserRole($user, $session) {

    if ($user->hasRole('teacher')) {                      
        $teacherPage = wire('pages')->get("template=teacher,user_id=$user->id");
        $teacherUrl = $teacherPage->httpUrl;
        $session->redirect($teacherPage->httpUrl);
    }

    if ($user->hasRole("student")) {
        $student = wire('pages')->get("template=student,user_id=$user->id");

        if ($student->game_id == 0) {            
            $game = wire('pages')->get("template=game,name=000");
            $gameUrl = $game->url;                     
        }
        else{
            $game = wire('pages')->get("template=game,id={$student->game_id}");
            $gameUrl = $game->url;           
        }     
         $session->redirect($game->url);
    }
}

// check for login before outputting markup
if ($input->post->username && $input->post->pass) {
    $username = $sanitizer->username($input->post->username);
    $pass = $input->post->pass;

    try {
        $user = $session->login($username, $pass);
        if (isset($user)) {
            getUserRole($user, $session);
        } else {
            echo "No user found with that name and password. Please try again";
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
?>
<p><a href="./teacheradmin/create-teacher-account/">Create an account </a></p><br />
<form action='./' method='post'>            
    <p>
        <label>Username <input type='text' name='username'></label>
        <label>Password <input type='password' name='pass'></label>
    </p>
    <button  type='submit'>Login</button>
    <p><a href='/reset-pass/'>Forgot your password?</a></p>

</form>

I deleted the page with id=2983 - it didn't make any difference. I was hoping for an enlightening error message.

I tried your suggestions of replacing .htaccess, wire directory and index.php.  I went back to earlier versions of the program ( replaced site directory with earlier versions) and the problem is still there - but I'm not sure at what stage it stopped working. The really early versions no longer work with the existing database. 

No custom hooks are running unless they are part of an installed module. Installed modules are Batcher, Selector test and Jquery Data Tables Plugin.

I'm pretty confident it's some sort of user error - I just don't know where to look for it, so I'm starting again with a clean copy of PW. Maybe I'll find some code I've forgotten about in the process.

Thanks for checking the problem out. I'll post again if something turns up.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...