We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39291
    • 19 Posts
    Hello,

    I crated a snippet with the "header location" function but it doesn't work. Why?

    header('Location:home.php');

    Thank you for a tip.
    Have a nice weekend
    Irène

    This question has been answered by multiple community members. See the first response.

    [ed. note: perinet last edited this post 14 years, 2 months ago.]
    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Is your friendly URL suffix .php, or .html?

      Anyway, in a snippet you should use the API function for that.

      $url = $modx->makeUrl(54);
      $modx->sendRedirect($url);


      http://rtfm.modx.com/display/revolution20/modX.sendRedirect
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Be sure you check your logs -- it's possible that headers were already sent, so your attempt to re-send new headers is failing.
          • 39291
          • 19 Posts
          Thank you for your responses. My friendly URL is .php

          It still doesn't work with the APl function for that. I just tried it.
          I tested my code in a website without modx and it worked perfectly.

          I don't understand... I use modx-2.2.4-pl
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            I'm not sure what your friendly URL has to do with it.

            Paste your entire Snippet code here please, and include the Snippet call you are using in your page or in your template to execute the Snippet.

            Re the header function, from the PHP docs:

            Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

            http://php.net/manual/en/function.header.php
              • 39291
              • 19 Posts
              Thank you for your answer. It's my session start:

              <?php
              session_start();
              if (isset($_POST['connexion']))
              {
              			if($_POST['login']=='goupil' || $_POST['pass']=='usa2012')
              		{
              			$_SESSION['login']='goupil';
              			$url = $modx->makeUrl(2);
              			$modx->sendRedirect($url);
              
              		}
              		else {
              			$erreur = '<p class="warning">Ihr Passwort ist falsch! Bitte versuchen Sie es erneut.</p>';
              		}
              }
              		
              		if (isset($_GET['logout']))
              		{
              		$success = '<p class="success">Sie haben sich erfolgreich ausgeloggt.</p>';	
              		}


              I call it with
              [[session_start?]]


              Do you see the problem?
              Thank you anyway
              [ed. note: perinet last edited this post 14 years, 2 months ago.]
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                So is this fixed?
                  • 39291
                  • 19 Posts
                  unfortunately no...
                  what is the problem? [ed. note: perinet last edited this post 14 years, 2 months ago.]
                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    You shouldn't need to call session_start() -- MODX relies on sessions already, so that's probably unnecessary.

                    Secondly, you should be testing for whether or not $_POST['login'] and $_POST['pass'] are set. My guess is that you'll see some helpful warnings/notices in your PHP logs.

                    I think you're problem is with your code, not necessarily with the API. If your snippet contained ONLY the following would it work?

                    $url = $modx->makeUrl(2);
                    $modx->sendRedirect($url);
                      • 39291
                      • 19 Posts
                      yes it work with only


                      $url = $modx->makeUrl(2);
                      $modx->sendRedirect($url);

                      You are right the problem is with my code, but I don't know where.

                      It's time to sleep now in Switzerland... I don't see anything anymore..

                      [ed. note: perinet last edited this post 14 years, 2 months ago.]